Get one quick Python tip each week Weekly Python advice: one quick Python tip every week

Equality vs identity in Python

Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
4 min. read Watch as video Python 3.8—3.11

You're probably already familiar with equality: that's the == operator. Identity uses the is operator.


Let's say we have two variables, x and y pointing to two lists:

>>> x = [1, 2, 3]
>>> y = [1, 2, 3]

If we say x == y we're asking about equality:

>>> x == y

Equality is about whether these two objects represent the same value, whether they're essentially the same thing.

The == operator delegates to one of these objects and asks it, "do you represent the same value as the other object?" That's up to the actual objects to answer.


The is operator asks about identity.

>>> x is y

Unlike ==, the is operator doesn't even look at the objects that x and y point to. Instead, is asks if the variables x and y are pointing to the same object.

The is operator answers the question do two object references actually point to the same object? The expression x is y checks the memory location that x and y are pointing to (by their id) and checks to see if those locations are the same.

>>> id(x)
>>> id(y)

If x and y have the same id in memory, that means we're referencing the same object in two places: x and y are actually referring to the same exact object.

In Python, assignment points a variable to an object.

If we assign x to y:

>>> x = y

We're now pointing the variable x to the same object that y is currently pointing to.

>>> id(x)
>>> id(y)

If we call the append method on the list that x points to


We've mutated that list object, but we've also mutated the object that y points to because both x and y point to exactly the same object.

>>> x
[1, 2, 3, 4]
>>> y
[1, 2, 3, 4]

These two objects are equal:

>>> x == y

But they're also identical (which means they point to the same object):

>>> x is y

Inequality and Non-identity

Just as we have equality (==) and inequality (!=):

>>> x == y
>>> x != y

we also have is for identity (is) and unidentity (is not)... or is it inidentity. How about non-identity.

>>> x is y
>>> x is not y

The is not operator is one of the few operators in Python that actually has a space inside it.

When is identity used?

You really don't see identity used very often. This is really the most important takeaway about identity and equality: you'll use == all the time, but you'll almost never use is. When comparing two objects, you'll almost always want to check for equality instead of identity.

The place you'll most commonly see is used is with None:

>>> x is None
>>> x is not None

, there are other places you might see it used, the one place you'll most commonly see it used

There's only one None value in memory in Python. We're asking the question "is x pointing to the one and only None value".

You'll only see is used with special values where there's only one of them in memory. I call these sentinel values. Sentinel objects are considered completely unique. There's only one of them floating in the memory.

Sentinel values are pretty much the one place you'll see identity used and None is by far the most common sentinel value in Python.

PEP 8, the Python style guide, says you should use identity to compare with None. So you should never x == None, but instead type x is None. That's the convention that we use with None in Python.


When you want to ask the question "does one object represent the same data as another object", you pretty much always want to use equality (with the == or != operators).

You'll almost never need to ask the question "is one pointer referencing literally the same object as another pointer". If you do want to ask that question though, you'll check identity (with the is and is not operators).

The one time that you really should rely on identity is when comparing to None (checking x is None).

Series: Assignment and Mutation

Python's variables aren't buckets that contain things; they're pointers that reference objects.

The way Python's variables work can often confuse folks new to Python, both new programmers and folks moving from other languages like C++ or Java.

To track your progress on this Python Morsels topic trail, sign in or sign up.

A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.