I have two list.
list1=["divyansh","jain","python"]
list2=["divyansh","jain","python"]
Both list have exactly the same data.
But when we apply the is identity operator it gives us false and also the memory address is different.
I know that in python everything is an object. If I declare another variable with same data value, both the variable points to the same object. Then why does this does not work in the case of list data type.
I know that python allocates default object at runtime for int values in range 0 to 255, boolean values & empty string.
Why is list2 pointing to some different memory address when list1 was created in first place with exact same values?
Code :
x=300
y=300
print("Memory Address of variable x is : ",id(x))
print("Memory Address of variable y is : ",id(y))
print(x is y)
print(x is not y)
list1=["divyansh","jain","python"]
list2=["divyansh","jain","python"]
print("Memory Address of variable list1 is : ",id(list1))
print("Memory Address of variable list2 is : ",id(list2))
print(list1 is list2)
print(list1 is not list2)
Output :
Memory Address of variable x is : 140185049313168
Memory Address of variable y is : 140185049313168
True
False
Memory Address of variable list1 is : 140185048064584
Memory Address of variable list2 is : 140185048053000
False
True
Creation of object is more time consuming and an expensive operation rather than searching for it and assigning the same memory address to it. I get it that for mutable objects its not possible , in future if we change list2 the list1 data will be effected and for this reason it created diff object. But for fundamental immutable data types like int,float,str its allocating same memory but in case of immutable data types like bytes,tuple,frozenset,complex,range it is not allocating the same memory although it has the same data value.
Other Data types example :
tuple1=("divyansh","jain","python")
tuple2=("divyansh","jain","python")
print("Memory Address of variable tuple1 is : ",id(tuple1))
print("Memory Address of variable tuple2 is : ",id(tuple2))
print(tuple1 is tuple2)
print(tuple1 is not tuple2)
bytes1=[1,2,3]
bytes2=[1,2,3]
b1=bytes(bytes1)
b2=bytes(bytes2)
print(type(b1),type(b2))
print(id(b1))
print(id(b2))
print(bytes1 is bytes2)
a=10.5
b=10.5
print(type(a),type(b))
print(id(a))
print(id(b))
print(a is b)
c1=10+20j
c2=10+20j
print(type(c1),type(c2))
print(id(c1))
print(id(c2))
print(c1 is c2)
s1="Dj"
s2="Dj"
print(type(s1),type(s2))
print(id(s1))
print(id(s2))
print(s1 is s2)
f1=frozenset({"Dj"})
f2=frozenset({"Dj"})
print(type(f1),type(f2))
print(id(f1))
print(id(f2))
print(f1 is f2)
r1=range(10)
r2=range(10)
print(type(r1),type(r2))
print(id(r1))
print(id(r2))
print(r1 is r2)
Memory Address of variable tuple1 is : 140088826761792
Memory Address of variable tuple2 is : 140088826762008
False
True
<class 'bytes'> <class 'bytes'>
140088827913280
140088827388192
False
<class 'float'> <class 'float'>
140088827982304
140088827982304
True
<class 'complex'> <class 'complex'>
140088827402864
140088827402896
False
<class 'str'> <class 'str'>
140088826759184
140088826759184
True
<class 'frozenset'> <class 'frozenset'>
140088827366088
140088827365640
False
<class 'range'> <class 'range'>
140088846540448
140088827189840
False
In the standard case of object programming: when you create a new object, the language/computer will reserve a new memory space and adress for it.
In Python when you create objects like list/containers, which are mutable objects, each one has its own memory address. So you can work with them and modify one without modifying the second one. This is necessary, if the adress was the same, it would be one same object, with two names/pointers.
This is neccessary to handle changes of the list1 and list2 without modifying the other. As lists are mutabe objects.
To check for content identity you have to check each item inside the lists. In Python use of '==' operator works for content comparison with, numbers, lists, tuples, ...
It is different than with numbers which are immutables objects in Python. As they are immutable and will never change, Python optimizes this by pointing to the same memory address when the same number is reused. (it is a kind of Singleton pattern)
This optimization choice was made because creating a new number object each time would have been to much time and memory consuming. And also the number of memory allocation/deallocation would have been too much also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With