Skip to content

What's the difference between is and == in Python? #386

Discussion options

You must be logged in to vote

Great question! This is a very common confusion in Python.


== checks if the values are equal
is checks if both variables point to the exact same object in memory


In your example:

a = [1, 2, 3]
b = [1, 2, 3]
a == bTrue because the contents of both lists are equal

a is bFalse because they are two different objects in memory

You can prove it by checking their IDs:

print(id(a))
print(id(b))

They'll be different!

Now check this:

x = [1, 2]
y = x
print(x is y)  # True → both point to the same object

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Istituto-freudinttheprodev
Comment options

Answer selected by Istituto-freudinttheprodev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants