Lessons · Python · two equal lists are not one list
Same value, or the same thing
== asks whether two values match. is asks whether both names point at the very same object.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Two separately loaded records can be equal without being the same one; two names for one list change together. Reading code that mixes them up is how phantom bugs get in.
How to think about it
Default to ==. Use is only for None (x is None) and for 'is this literally the same object'. If you cannot say which object you mean, you mean ==.
Worked example
a = [1, 2]One list.
b = [1, 2]Built separately.
print(a == b)True: same contents.
print(a is b)False: two lists.
c = aA second name for the first list.
print(c is a)True: one object, two names.
Your turn
Has the value been left unset?
missing = value None
Solve one with the tests running
The trap
x == None works but x is None is the idiom, because a custom == can lie. For everything else, is is the wrong question.
Practise two equal lists are not one list on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.