Why does the result of id() appear to be not unique in Python?


The id() method in Python return the identity of an object i.e. a unique id for the specified object. Now, you would be wondering, what is this id().

The id here is the object's memory address, an integer guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Syntax

id(object)

This object can be object, String, Number, List, etc.

Unique id of List Object

Example

In this example, we will get the unique id of the list object using the id() −

myList = ["john", "tom", "henry", "mark"] res = id(myList) print(res)

Output

140571958913920

The id will be different when we will run it again:

140597372271552

Unique id of Tuple Object

Example

In this example, we will get the unique id of the Tuple object using the id() method −

myTuple = ("david", "steve", "alexa", "dwyer") res = id(myTuple) print(res)

Output

140389997162960

The id will be different when we will run it again −

140674820137424

Unique id of integers

Example

In this example, we will get the unique id of integers −

print(id(50)) print(id(100))

Output

140184574995904
140184574997504

Updated on: 19-Sep-2022

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements