Vinay Garg Assistant Professor

Lesson 11: Membership and identity operators: in, not in, is, is not

Membership asks whether a value is inside a collection. Identity asks whether two names point at the same object. They look similar and answer completely different questions.

A dark code window showing a membership test on a list and an identity test

In short

  • `in` and `not in` test whether a value is contained in a string, list, tuple, dictionary or set.
  • `is` and `is not` test whether two names point at the same object, not whether they hold equal values.
  • Use `==` for values and `is` only for `None`, `True` and `False`.
  • On a dictionary, `in` looks at the keys, not the values.

2

membership operators: in, not in

Python docs

2

identity operators: is, is not

Python docs

Membership: is the value inside?

The membership operators are in and not in. They ask a yes/no question about a collection and return True or False.

print("py" in "python")        # True, substring of the text
print("java" not in "python")  # True

marks = [45, 67, 89]
print(67 in marks)             # True

student = {"name": "Asha", "roll": 21}
print("name" in student)       # True, keys are tested

Identity: is it the same object?

Every value in Python lives somewhere in memory. The identity operators is and is not ask whether two names point at that same place. This is a different question from "do they hold equal values".

a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)     # True: same values
print(a is b)     # False: two separate lists
print(a is c)     # True: both names point at one list

The program

# membership.py — in, not in, is and is not in one place

text = "Python is easy to read"
print("easy" in text)          # True
print("hard" not in text)      # True

numbers = [10, 20, 30]
print(20 in numbers)           # True
print(40 in numbers)           # False

marks = {"maths": 88, "science": 91}
print("maths" in marks)        # True, the key
print(88 in marks)             # False, values are not tested

letters = {"a", "b"}
print("b" in letters)          # True

a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b, a is b)          # True False
print(a is c)                  # True

value = None
print(value is None)           # True, the usual test for None

How it works, step by step

  1. "easy" in text searches inside the string. For text, membership means "is this a substring".
  2. 20 in numbers walks the list and compares each item with 20.
  3. "maths" in marks checks the dictionary's keys. To test a value you would write 88 in marks.values().
  4. a == b compares element by element, so two separate lists with the same items are equal.
  5. a is b compares the objects, so it is False: they were created by two different statements.
  6. a is c is True because c = a copies the reference, not the list.

What it prints

True
True
True
False
True
False
True
True False
True
True

Which one should you use

Use in constantly — it is clear and fast enough for normal sizes. Use is only for None, True and False, where it is the recommended test. For everything else, compare with ==; two equal numbers or strings may or may not be the same object, and the answer can differ between Python versions.

Common mistakes

  • if x is 5 — use x == 5. Small integers are cached and it happens to work, then fails for large ones.
  • if "maths" in marks expecting the value 88. It tests keys only.
  • Using in on a set of lists: sets need hashable items, so a list inside a set fails at creation.

Practice

Try these before moving on. Each one uses only what this lesson covered.

  1. Check whether the word "python" appears in a sentence you store in a variable.
  2. Store five subject names in a list and print whether "Maths" is in it.
  3. Create two lists with the same values and print both `==` and `is` results, then explain the difference in a comment.

Sources

Frequently asked questions

What is the difference between == and is in Python?

== compares values, so two separate lists with the same items are equal. is compares objects, so those two lists are different objects and is returns False. Use == unless you are testing None.

What does the in operator do?

It tests membership: whether a value appears inside a string, list, tuple, dictionary or set. It returns True or False and can be negated with not in.

Does in work on dictionary values?

No. On a dictionary, in checks the keys only. To test a value, write 88 in marks.values().

Why does x is None work but x is 5 does not?

None is a single object, so identity is the right and recommended test. The number 5 may be represented by several objects, and small-integer caching makes is unreliable for numbers.

Vinay Garg

Vinay Garg

Assistant Professor of Computer Science at Multani Mal Modi College, Patiala. Twenty-five years of teaching web programming, and a habit of building the things I teach about.