Python is vs ==: what's the difference?
Equal lists can still be different objects. Choose between is and == in Python, follow aliases, and avoid comparison traps with None, strings and integers.
Two lists display the same contents, yet a is b returns False. A plain assignment can then produce True. These comparisons ask different questions about the objects.
In short
In Python, == tests equality as defined by the objects' types; for two lists of integers, it compares their contents. is tests whether both operands refer to the same object. Use == to compare values, is None to recognize a missing value, and is when your contract specifically requires the same object. Separate objects can be equal.
Why aren't two equal lists the same object?
Each list expression here creates a separate object. The assignment alias = a gives another name to the existing list.
a = [1, 2]b = [1, 2]alias = aprint(a == b)print(a is b)print(a is alias)alias.append(3)print(a)print(b)print(a == b)$ python3 lists.pyTrueFalseTrue[1, 2, 3][1, 2]FalseThe mutation goes through alias, but you can see it through a: both names refer to the same list. b hasn't changed.
Stack
- lista
- list-a
- listalias
- list-a
- listb
- list-b
Heap
This diagram shows references between objects, not Python's physical memory layout. For copies that contain other lists, read copying nested lists.
How do you check that a result is a new list?
Check contents and identity separately. Equality alone cannot tell you whether a function returned its input or a copy.
def copy_values(values): return values.copy()
for original in ([], [1, 2], [1, 1]): result = copy_values(original) print(result == original, result is original)$ python3 copy.pyTrue FalseTrue FalseTrue FalseEven a copy of the empty list is a separate object. The test result is not original expresses that requirement; result != original would ask for different contents instead.
For a function that adds a value without changing its input, the two lists no longer have equal contents. Check the expected contents, the separate identity and the preserved input. These checks establish different properties.
with_appended
Implement with_appended: return a new list holding the input items followed by the added value. The evaluator also checks that the input is unchanged and the result is a separate object, including when the input is empty.
Why does is seem to work on some strings or integers?
Python may reuse an immutable object for equal values. The official FAQ explains that integers and strings aren't necessarily singletons: equal values don't guarantee identical objects.
A quick experiment with two literals can therefore make is look like a content comparison. Its result may depend on the implementation and how the code is constructed. Don't build a comparison rule around an integer threshold or a string length.
To compare entered or computed names, write name == expected. To check a number, write number == limit. You don't need to control whether the interpreter shares their objects.
Why use is None instead of == None?
None is a unique value. An identity test recognizes that object without calling a custom equality comparison.
A class can decide that an instance is equal to other objects. This deliberately misleading example shows what == allows:
class AlwaysEqual: def __eq__(self, other): return True
value = AlwaysEqual()print(value == None)print(value is None)print(None is None)print([] is None)$ python3 missing.pyTrueFalseTrueFalseThe instance isn't None, even though its equality method returns True. That method cannot redefine is. For custom objects, saying that == always compares every field would be wrong: their type determines the comparison.
The test if not value asks another question: is the value considered false? It also accepts an empty list, zero or an empty string. When only a missing value should trigger a behavior, use if value is None. This distinction also helps fix mutable default arguments.
Frequently asked questions
Are is not and != interchangeable?
No. a is not b means the objects are separate. a != b asks for an inequality comparison. Two separately created lists with the same contents are separate without being unequal.
Should I use is to make comparisons faster?
Choose the right question first. An identity test that violates your contract remains incorrect even if it is fast. Use equality to compare list or string contents.
Does is compare types?
No. Two lists have the same type without being the same object. Conversely, numeric values of different types can compare equal: equality isn't an exact type check.
Can a class change how is works?
No. A class can customize equality through comparison methods, but it cannot redefine the identity operator.
Sources
