In Python, you can use the in operator to check if a value exists in a group of values.
For example:
>>> "Alice" in ["Alice", "Bob", "Charlie"] True >>> "H" in "Hello world" True
Similarly, you can check if a value is not in a collection with not in operation (combining the not operator and the in operator):
>>> "David" not in ["Alice", "Bob", "Charlie"] True
The ‘in’ Operator in Python
The in operator works with iterable types, such as lists or strings, in Python. It is used to check if an element is found in the iterable. The in operator returns True if an element is found. It returns False if not.
For example, let’s check if “Charlie” is in a list of names:
>>> names = ["Alice", "Bob", "Charlie"] >>> if "Charlie" in names: ... print("Charlie found") ... Charlie found
The “not in” Operator in Python
Another common way to use the in operator is to check if a value is not in a group.
To do this, you can negate the in operator with the not operator to give rise to the not in operator.
For example, let’s check if there are no “s” letters in a word:
>>> word = "Hello world" >>> if "s" not in word: ... print("No 's' letters found!") ... No 's' letters found!
When Use the ‘in’ Operator in Python
Use the in operator whenever you want to check if an iterable object contains a value.
Commonly, you see the in operator combined with an if operator.
Examples
Let’s see some common example use cases for the in operator in Python.
Check If a Substring Exists in a String
As you learned, you can use the in operator with iterables in Python. A Python string is an iterable object. This means you can use the in operator on a string as well.
When using the in operator on a string, you can check if:
- A character exists in a string.
- A substring exists in a string.
For example, let’s see if “Hello” exists in “Hello world”:
>>> "Hello" in "Hello world" True
Check If a Key Exists in a Dictionary
In Python, a dictionary is an indexed collection of key-value pairs unlike lists or strings for example.
As you may know, you can access key-value pairs in the dictionary using keys. So it is the key that gives you access to the dictionary. Similarly, to check if a key-value pair exists in a dictionary, you need to check if the key exists.
For example:
>>> data = {"age": 30, "name": "Bob"} >>> "age" in data True
This can be useful if you want to safely access a dictionary with the square brackets operator:
>>> data = {"age": 30, "name": "Bob"} >>> if "age" in data: ... print(f"He is {data['age']} years old.") ... He is 30 years old.
If a key-value pair does not exist in the dictionary and you try to access it this way, you would see an error.
For example:
>>> data = {"age": 30, "name": "Bob"} >>> print(f"He's nickname is {data['nickname']}") Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'nickname'
So you should always make sure the key-value pair exists before accessing it with the square brackets operator.
Check If a Value Exists in a List
A really common way to use the in operator is to check if a value exists in a list.
For example, let’s see if a specific name exists in a list of names:
>>> students = ["Alice", "Bob", "Charlie"] >>> "Charlie" in students True
Conclusion
Today you learned how to use the in operator to check if a value exists in an iterable in Python.
To recap, the in operator works with any iterable types. You can use it to check if an iterable has a specific value. When you want to check if a value does not exist, just chain the in operator with a not operator to form a not in operator.
Thanks for reading.
Happy coding!