Membership operators check whether a value is present inside a collection such as a list, string, tuple, set, dictionary or range. Python has two of them, in and not in, and both return True or False.
They make code read almost like English: if "mango" in fruits: means exactly what it says.
Membership Operators in Python
| Operator | Returns True when | Example |
|---|---|---|
in |
the value is found in the collection | "a" in "cat" gives True |
not in |
the value is not found in the collection | 5 not in [1, 2, 3] gives True |
With Lists
fruits = ["apple", "mango", "banana"]
print("mango" in fruits)
print("grapes" in fruits)
print("grapes" not in fruits)
Output:
True
False
True
With Strings
For strings, in checks for a substring, so it can find whole words or parts of words. The check is case-sensitive.
sentence = "Python is easy to learn"
print("easy" in sentence)
print("Easy" in sentence)
print("" in sentence)
Output:
True
False
True
An empty string is considered to be inside every string, which is why the last line is True. For a case-insensitive check, convert both sides: "Easy".lower() in sentence.lower().
With Tuples and Sets
vowels = ("a", "e", "i", "o", "u")
print("e" in vowels)
primes = {2, 3, 5, 7, 11}
print(9 in primes)
Output:
True
False
Sets are the fastest choice for membership checks. Python checks a set or a dictionary almost instantly, however large it is, while a list has to be scanned item by item. If you need to check membership many times in a big collection, convert the list to a set first.
With Dictionaries
With a dictionary, in checks the keys, not the values. Use .values() or .items() when you need those.
marks = {"Riya": 91, "Aman": 78}
print("Riya" in marks)
print(91 in marks)
print(91 in marks.values())
print(("Aman", 78) in marks.items())
Output:
True
False
True
True
With range()
print(50 in range(1, 101))
print(100 in range(1, 100))
Output:
True
False
range(1, 100) stops before 100, so 100 is not in it.
Practical Examples
1. Count the Vowels in a Word
word = "education"
count = 0
for ch in word:
if ch in "aeiou":
count += 1
print(count)
Output:
5
2. Validate a Menu Choice
choice = "3"
if choice in ("1", "2", "3"):
print("Valid choice")
else:
print("Invalid choice")
Output:
Valid choice
This is much cleaner than writing choice == "1" or choice == "2" or choice == "3".
3. Remove Duplicates While Keeping the Order
names = ["Riya", "Aman", "Riya", "Kabir", "Aman"]
unique = []
for n in names:
if n not in unique:
unique.append(n)
print(unique)
Output:
['Riya', 'Aman', 'Kabir']
Watch Out: Nested Lists
in only looks at the top level of a collection. It does not search inside inner lists.
matrix = [[1, 2], [3, 4]]
print(3 in matrix)
print([3, 4] in matrix)
print(any(3 in row for row in matrix))
Output:
False
True
True
The items of matrix are the two inner lists, so 3 in matrix is False. To search inside every row, use any() as in the last line.
Practice Questions
print("a" in "Banana")print("ana" in "Banana")print(2 in [[1, 2], [3]])
Answers: 1) True. “Banana” contains a small “a” at index 1. 2) True. “ana” appears as a substring. 3) False. The list holds two inner lists, not the number 2.
Frequently Asked Questions
What are membership operators in Python?
in and not in. They test whether a value is present in a sequence or collection.
Does in check keys or values in a dictionary?
Keys. Use value in my_dict.values() to check values.
Is the in operator case-sensitive for strings?
Yes. "Easy" in "Python is easy" is False. Convert both strings to lower case for a case-insensitive check.
What is the difference between in and ==?
== compares two whole values. in checks whether one value is contained inside another.
Next: Identity operators in Python · Previous: Python bitwise operators · All operators: Python operators overview