+ 67

a = [1,2,3,4] , b = [1,2,3,4] . How are they not same

a == b returns True Then why ‘a is b’ returns False? a and b are the same lists after all !

18th Nov 2019, 6:48 PM
Anushka Mehta
134 Réponses
+ 272
Imagine a and b as twins: They're equal, but they're still two separate 'persons'.
18th Nov 2019, 7:30 PM
HonFu
HonFu - avatar
+ 162
Write in your interpreter print(id(a)) print (id(b)) and you will see that a and b are 2 objects that have different addresses in memory
19th Nov 2019, 8:22 PM
Vlad
Vlad - avatar
+ 50
I understand those lists are different objects, so they have their own id and point to different addresses of memory, for that reason a is not b. If you change a, b will still being [1,2,3,4]
18th Nov 2019, 8:01 PM
Sebastián Zapata
Sebastián Zapata - avatar
+ 42
Hi Loay I agree with all but I want to add some to your comment, for avoiding a possible misunderstanding. The case when we let x = n y = n With n a number and not another variable (this is important), if our n is any integer between -5 and 256, so "x is y" returns true because they REALLY are the same object and will always have the same id for every number in there. But any other integer will return false for "x is y" because they will be different objects. Examples: i = 256 j = 256 i is j >>> True x = 257 y = 257 x is y >>> False a = 300 b = a a is b >>> True https://docs.python.org/3/c-api/long.html (I know, that says "long" but the explanation is inside :P) Plz if I have some mistake I'm very thankful if someone corrects me, keep coding!
19th Nov 2019, 11:02 AM
Sebastián Zapata
Sebastián Zapata - avatar
+ 30
By human language, I think it can be translated as something like this: I have two hands. `a = [1, 2, 3, 4]` You have two hands. `b = [1, 2, 3, 4]` We both have two hands. `a == b` But my hands are not your hands. `a is not b`
19th Nov 2019, 12:05 PM
o.gak
o.gak - avatar
+ 22
from docs.python.org ✅ every object has an identity, a type and a value. ⚠️ object identity is object’s address in memory. ✅ ‘is’ operator compares the identity of two objects 🙏🏻 so even if two objects have same value but they are not same identity .
16th Mar 2021, 10:36 PM
Mohammed Jaafar
Mohammed Jaafar - avatar
+ 11
Have a look at this :- https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_operators.asp ....about half way down the page.
18th Nov 2019, 7:10 PM
rodwynnejones
rodwynnejones - avatar
+ 10
As stated they are different locations in memory.
18th Nov 2019, 9:02 PM
Inkosi
Inkosi - avatar
+ 10
Here "is" operator is an identity operator whereas "==" is an assignment operator. Identity operator is used to compare the memory locations of both the variables or operands.If they have identical memory location then it will return True as an output. Assignment operator is used to compare the vaalues of both the variables, if they have identical values then it will return True as an output
24th Aug 2021, 7:02 AM
Riya Kumari
Riya Kumari - avatar
+ 8
Equal Values of two variables can't make them same....... like age of A and age of B are equal but that doesn't mean that A and B are same person ....like this value of a and value of b are same but they are still having different identity (address).....so they're not same !!!
20th Nov 2019, 6:02 PM
Partha Sarathi
Partha Sarathi - avatar
+ 6
Many already wrote here: a is not b. They are different objects, that in this Case hold the same Value. But this Brings me to Another Topic: If you want to duplicate a list, Never try to do it like this: b = [1,2,3,4] a = b Because in this case the lists will refer to the same space in memory. They will really be the same. And Changing a after that will Change b too. Instead do: a = b.copy()
30th Jun 2021, 8:38 AM
Ann
Ann - avatar
+ 4
Please, guys, at least READ the question before you answer it (for the n-th time). This is about PYTHON!
20th Nov 2019, 10:17 AM
HonFu
HonFu - avatar
+ 2
When we let x = 1 y = 1 both x== y & x is y return true as immutable objects in python share the same memory Like definig y as a reference for x in other languages but they are not a reference just when they share the same value memory management tends to link them to point to one shared address i.e x = 1, y= x x = 3 y still equal to 1 To make that clear (1, 2, 3) is (1, 2, 3) return true But for list which is mutable each created list is nothing but a linkedList object have different pointers in memory blindly can't see other objects that share their same value but in other addresses You can use id(x), id(y) that will help you see each object memory address
19th Nov 2019, 10:29 AM
Loay Wael
Loay Wael - avatar
+ 2
Same contents but the variables are different.
21st Nov 2019, 2:49 AM
Suhail 🐈[INACTIVE]
Suhail 🐈[INACTIVE] - avatar
+ 2
The equal operators make a == b True so in the statement 'a is b' is does not give value to the variables 'a' and 'b'.
19th Jul 2021, 4:27 PM
Gomolemo Mathabathe
Gomolemo Mathabathe - avatar
+ 2
I think may be memory allocated is different between a and b, even we seen the list is same.
23rd Jul 2021, 12:57 PM
Zy Naung
Zy Naung - avatar
+ 2
Here, a==b is always true, but they are totally unique, and has different addresses, so they aren't same
19th Sep 2021, 11:37 AM
Siriki Lohit
Siriki Lohit - avatar
+ 2
a=[1,2,3] b=[1,2,3] print(a==b) //equal thier values like [1,2,3] print(a is b) //equal like objects a and b, they are different print(id(a)) print(id(b))
3rd Dec 2021, 5:16 PM
Dmytro Tverdovskyi
+ 2
Hello everyone, with „==„ you compare the lists and they are actually equal, that‘s why the output would be True if you print it. But a = b means that you assign the value of b to the variable a ( In other words you give variable a the same value as b) If you print(a) now the result will be the same as b because you gave a the same value as b. I hope my explanation is helpful.
8th Feb 2022, 10:23 AM
Ferdaws Kukcha
Ferdaws Kukcha - avatar
+ 2
Imagine a and b as twins: They're equal, but they're still two separate 'persons'.
17th Feb 2022, 9:40 AM
Ehem
Ehem - avatar