id == id | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

id == id

a,b = [1,2] [1,2] if id(a) == id(b): print("y") else: print("n") Solution: n a is assigned [1,2] b is assigned [1,2] values of a and b renamed to id(a) and id(b): So confused on why they are not ==. Thanks:).

29th Mar 2019, 6:40 AM
tristach605
tristach605 - avatar
7 Answers
+ 9
Because they are two seperate objects of list class in the memory and computer have given them different names or 'id' to recognize them. Even though their 'values' are same but their 'identity' or simply 'id' is not equal...... 😄 you can print the ids of them using the print function and can see they aren't equal...... ex: - print(id(a), id(b))
30th Mar 2019, 1:13 PM
Ayush Sinha
Ayush Sinha - avatar
+ 7
If you are going to do variable initialisation this way: a = [1,2] b = a a and be will have the same content and also the same object id. So there is only 1 object but 2 vars that pointed to the object.
29th Mar 2019, 9:57 AM
Lothar
Lothar - avatar
+ 7
Ids are a bit so similar to memory addresses in languages like C.
29th Mar 2019, 11:12 PM
Sonic
Sonic - avatar
+ 5
Although a and b have same value but the two are different objects and hence will have different ids.
29th Mar 2019, 7:27 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 4
[1,2] are the values of a and b, NOT the values of their id. Like Shashi Ranjan said, they're different objects, so they possess different id's, regardless of their values.
29th Mar 2019, 7:40 AM
Ruben Lebre
Ruben Lebre - avatar
+ 1
Use missed comma in the initialization.
29th Mar 2019, 7:13 AM
Adarsh.n. Bidari
Adarsh.n. Bidari - avatar
+ 1
Thanks everyone! I'll review Sololearn to practice. In the meantime, I'm still a bit confused. Can anyone provide any other examples, etc. so I could practice? Thanks.
29th Mar 2019, 9:51 PM
tristach605
tristach605 - avatar