We are given a String and our task is to test if the string contains elements from the list.
Example:
Input: String: Geeks for Geeks is one of the best company. List: ['Geeks', 'for'] Output: Does string contain any list element : True
Naive Approach checking each word in the string
Here we are splitting the string into list of words and then matching each word of this list with the already present list of words we want to check.
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
test_string
=
test_string.split(
" "
)
flag
=
0
for
i
in
test_string:
for
j
in
test_list:
if
i
=
=
j:
flag
=
1
break
if
flag
=
=
1
:
print
(
"String contains the list element"
)
else
:
print
(
"String does not contains the list element"
)
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] String contains the list element
Using list comprehension to check if string contains element from list
This problem can be solved using the list comprehension, in this, we check for the list and also with string elements if we can find a match, and return true, if we find one and false is not using the conditional statements.
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
res
=
[ele
for
ele
in
test_list
if
(ele
in
test_string)]
print
(
"Does string contain any list element : "
+
str
(
bool
(res)))
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Using any() to check if string contains element from list
Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
res
=
any
(ele
in
test_string
for
ele
in
test_list)
print
(
"Does string contain any list element : "
+
str
(res))
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Using find() method to check if string contains element from list
Here we are using the find() method to check the occurrence of the word and it returns -1 if the word does not exist in the list.
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
res
=
False
c
=
0
for
i
in
test_list:
if
(test_string.find(i)!
=
-
1
):
c
+
=
1
if
(c>
=
1
):
res
=
True
print
(
"Does string contain any list element : "
+
str
(
bool
(res)))
Output:
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Using Counter() function
Python3
from
collections
import
Counter
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
res
=
False
freq
=
Counter(test_list)
for
i
in
test_string.split():
if
i
in
freq.keys():
res
=
True
print
(
"Does string contain any list element : "
+
str
(
bool
(res)))
Output
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Time Complexity: O(N)
Auxiliary Space: O(N)
Using Set Intersection to Check if String Contains Element from List
One approach to check if a string contains an element from a list is to convert the string and the list into sets and then check for the intersection between the sets. If the intersection is not an empty set, it means that the string contains an element from the list.
Python3
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string:"
, test_string)
print
(
"The original list:"
, test_list)
string_set
=
set
(test_string.split())
list_set
=
set
(test_list)
if
list_set.intersection(string_set):
print
(
"String contains an element from the list"
)
else
:
print
(
"String does not contain an element from the list"
)
Output
The original string: There are 2 apples for 4 persons The original list: ['apples', 'oranges'] String contains an element from the list
This approach has a time complexity of O(n) and an auxiliary space of O(n).
Using operator.countOf() method.
Python3
import
operator as op
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
res
=
False
c
=
0
for
i
in
test_list:
if
(op.countOf(test_string, i)
=
=
0
):
c
+
=
1
if
(c >
=
1
):
res
=
True
print
(
"Does string contain any list element : "
+
str
(
bool
(res)))
Output
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Using operator.contains() method
Approach
- Initiate a for loop to traverse list of strings and set res to False
- Check whether each string of list is present in given string
- If yes set res to True and break out of for loop
- Display res
Python3
import
operator as op
test_string
=
"There are 2 apples for 4 persons"
test_list
=
[
'apples'
,
'oranges'
]
print
(
"The original string : "
+
test_string)
print
(
"The original list : "
+
str
(test_list))
res
=
False
c
=
0
for
i
in
test_list:
if
(op.contains(test_string, i)):
res
=
op.contains(test_string, i)
break
print
(
"Does string contain any list element : "
+
str
(res))
Output
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Time Complexity : O(M*N) M-length of test_list N-length of test_string
Auxiliary Space : O(1)
Python’s in
and not in
operators allow you to quickly determine if a given value is or isn’t part of a collection of values. This type of check is common in programming, and it’s generally known as a membership test in Python. Therefore, these operators are known as membership operators.
In this tutorial, you’ll learn how to:
- Perform membership tests using the
in
andnot in
operators - Use
in
andnot in
with different data types - Work with
operator.contains()
, the equivalent function to thein
operator - Provide support for
in
andnot in
in your own classes
To get the most out of this tutorial, you’ll need basic knowledge of Python, including built-in data types, such as lists, tuples, ranges, strings, sets, and dictionaries. You’ll also need to know about Python generators, comprehensions, and classes.
Getting Started With Membership Tests in Python
Sometimes you need to find out whether a value is present in a collection of values or not. In other words, you need to check if a given value is or is not a member of a collection of values. This kind of check is commonly known as a membership test.
Arguably, the natural way to perform this kind of check is to iterate over the values and compare them with the target value. You can do this with the help of a for
loop and a conditional statement.
Consider the following is_member()
function:
>>>
>>> def is_member(value, iterable):
... for item in iterable:
... if value is item or value == item:
... return True
... return False
...
This function takes two arguments, the target value
and a collection of values, which is generically called iterable
. The loop iterates over iterable
while the conditional statement checks if the target value
is equal to the current value. Note that the condition checks for object identity with is
or for value equality with the equality operator (==
). These are slightly different but complementary tests.
If the condition is true, then the function returns True
, breaking out of the loop. This early return short-circuits the loop operation. If the loop finishes without any match, then the function returns False
:
>>>
>>> is_member(5, [2, 3, 5, 9, 7])
True
>>> is_member(8, [2, 3, 5, 9, 7])
False
The first call to is_member()
returns True
because the target value, 5
, is a member of the list at hand, [2, 3, 5, 9, 7]
. The second call to the function returns False
because 8
isn’t present in the input list of values.
Membership tests like the ones above are so common and useful in programming that Python has dedicated operators to perform these types of checks. You can get to know the membership operators in the following table:
Operator | Description | Syntax |
---|---|---|
in |
Returns True if the target value is present in a collection of values. Otherwise, it returns False . |
value in collection |
not in |
Returns True if the target value is not present in a given collection of values. Otherwise, it returns False . |
value not in collection |
As with Boolean operators, Python favors readability by using common English words instead of potentially confusing symbols as operators.
Like many other operators, in
and not in
are binary operators. That means you can create expressions by connecting two operands. In this case, those are:
- Left operand: The target value that you want to look for in a collection of values
- Right operand: The collection of values where the target value may be found
The syntax of a membership test looks something like this:
value in collection
value not in collection
In these expressions, value
can be any Python object. Meanwhile, collection
can be any data type that can hold collections of values, including lists, tuples, strings, sets, and dictionaries. It can also be a class that implements the .__contains__()
method or a user-defined class that explicitly supports membership tests or iteration.
If you use the in
and not in
operators correctly, then the expressions that you build with them will always evaluate to a Boolean value. In other words, those expressions will always return either True
or False
. On the other hand, if you try and find a value in something that doesn’t support membership tests, then you’ll get a TypeError
. Later, you’ll learn more about the Python data types that support membership tests.
Because membership operators always evaluate to a Boolean value, Python considers them Boolean operators just like the and
, or
, and not
operators.
Now that you know what membership operators are, it’s time to learn the basics of how they work.
Python’s in
Operator
To better understand the in
operator, you’ll start by writing some small demonstrative examples that determine if a given value is in a list:
>>>
>>> 5 in [2, 3, 5, 9, 7]
True
>>> 8 in [2, 3, 5, 9, 7]
False
The first expression returns True
because 5
appears inside your list of numbers. The second expression returns False
because 8
isn’t present in the list.
According to the in
operator documentation, an expression like value in collection
is equivalent to the following code:
any(value is item or value == item for item in collection)
The generator expression wrapped in the call to any()
builds a list of the Boolean values that result from checking if the target value
has the same identity or is equal to the current item
in collection
. The call to any()
checks if any one of the resulting Boolean values is True
, in which case the function returns True
. If all the values are False
, then any()
returns False
.
Python’s not in
Operator
The not in
membership operator does exactly the opposite. With this operator, you can check if a given value is not in a collection of values:
>>>
>>> 5 not in [2, 3, 5, 9, 7]
False
>>> 8 not in [2, 3, 5, 9, 7]
True
In the first example, you get False
because 5
is in [2, 3, 5, 9, 7]
. In the second example, you get True
because 8
isn’t in the list of values. This negative logic may seem like a tongue twister. To avoid confusion, remember that you’re trying to determine if the value is not part of a given collection of values.
With this quick overview of how membership operators work, you’re ready to go to the next level and learn how in
and not in
work with different built-in data types.
Using in
and not in
With Different Python Types
All built-in sequences—such as lists, tuples, range
objects, and strings—support membership tests with the in
and not in
operators. Collections like sets and dictionaries also support these tests. By default, membership operations on dictionaries check whether the dictionary has a given key or not. However, dictionaries also have explicit methods that allow you to use the membership operators with keys, values, and key-value pairs.
In the following sections, you’ll learn about a few particularities of using in
and not in
with different built-in data types. You’ll start with lists, tuples, and range
objects to kick things off.
Lists, Tuples, and Ranges
So far, you’ve coded a few examples of using the in
and not in
operators to determine if a given value is present in an existing list of values. For these examples, you’ve explicitly used list
objects. So, you’re already familiar with how membership tests work with lists.
With tuples, the membership operators work the same as they would with lists:
>>>
>>> 5 in (2, 3, 5, 9, 7)
True
>>> 5 not in (2, 3, 5, 9, 7)
False
There are no surprises here. Both examples work the same as the list-focused examples. In the first example, the in
operator returns True
because the target value, 5
, is in the tuple. In the second example, not in
returns the opposite result.
For lists and tuples, the membership operators use a search algorithm that iterates over the items in the underlying collection. Therefore, as your iterable gets longer, the search time increases in direct proportion. Using Big O notation, you’d say that membership operations on these data types have a time complexity of O(n).
If you use the in
and not in
operators with range
objects, then you get a similar result:
>>>
>>> 5 in range(10)
True
>>> 5 not in range(10)
False
>>> 5 in range(0, 10, 2)
False
>>> 5 not in range(0, 10, 2)
True
When it comes to range
objects, using membership tests may seem unnecessary at first glance. Most of the time, you’ll know the values in the resulting range beforehand. But what if you’re using range()
with offsets that are determined at runtime?
Consider the following examples, which use random numbers to determine offsets at runtime:
>>>
>>> from random import randint
>>> 50 in range(0, 100, randint(1, 10))
False
>>> 50 in range(0, 100, randint(1, 10))
False
>>> 50 in range(0, 100, randint(1, 10))
True
>>> 50 in range(0, 100, randint(1, 10))
True
On your machine, you might get different results because you’re working with random range offsets. In these specific examples, step
is the only offset that varies. In real code, you could have varying values for the start
and stop
offsets as well.
For range
objects, the algorithm behind the membership tests computes the presence of a given value using the expression (value - start) % step) == 0
, which depends on the offsets used to create the range at hand. This makes membership tests very efficient when they operate on range
objects. In this case, you’d say that their time complexity is O(1).
Remember that the target value in a membership test can be of any type. The test will check if that value is or isn’t in the target collection. For example, say that you have a hypothetical app where the users authenticate with a username and a password. You can have something like this:
# users.py
username = input("Username: ")
password = input("Password: ")
users = [("john", "secret"), ("jane", "secret"), ("linda", "secret")]
if (username, password) in users:
print(f"Hi {username}, you're logged in!")
else:
print("Wrong username or password")
This is a naive example. It’s unlikely that anyone would handle their users and passwords like this. But the example shows that the target value can be of any data type. In this case, you use a tuple of strings representing the username and the password of a given user.
Here’s how the code works in practice:
$ python users.py
Username: john
Password: secret
Hi john, you're logged in!
$ python users.py
Username: tina
Password: secret
Wrong username or password
In the first example, the username and password are correct because they’re in the users
list. In the second example, the username doesn’t belong to any registered user, so the authentication fails.
In these examples, it’s important to note that the order in which the data is stored in the login tuple is critical because something like ("john", "secret")
isn’t equal to ("secret", "john")
in tuple comparison even if they have the same items.
In this section, you’ve explored examples that showcase the core behavior of membership operators with common Python built-in sequences. However, there’s a built-in sequence left. Yes, strings! In the next section, you’ll learn how membership operators work with this data type in Python.
Strings
Python strings are a fundamental tool in every Python developer’s tool kit. Like tuples, lists, and ranges, strings are also sequences because their items or characters are sequentially stored in memory.
You can use the in
and not in
operators with strings when you need to figure out if a given character is present in the target string. For example, say that you’re using strings to set and manage user permissions for a given resource:
>>>
>>> class User:
... def __init__(self, username, permissions):
... self.username = username
... self.permissions = permissions
...
>>> admin = User("admin", "wrx")
>>> john = User("john", "rx")
>>> def has_permission(user, permission):
... return permission in user.permissions
...
>>> has_permission(admin, "w")
True
>>> has_permission(john, "w")
False
The User
class takes two arguments, a username and a set of permissions. To provide the permissions, you use a string in which w
means that the user has write permission, r
means that the user has read permission, and x
implies execution permissions. Note that these letters are the same ones that you’d find in the Unix-style file-system permissions.
The membership test inside has_permission()
checks whether the current user
has a given permission
or not, returning True
or False
accordingly. To do this, the in
operator searches the permissions string to find a single character. In this example, you want to know if the users have write permission.
However, your permission system has a hidden issue. What would happen if you called the function with an empty string? Here’s your answer:
>>>
>>> has_permission(john, "")
True
Because an empty string is always considered a substring of any other string, an expression like "" in user.permissions
will return True
. Depending on who has access to your users’ permissions, this behavior of membership tests may imply a security breach in your system.
You can also use the membership operators to determine if a string contains a substring:
>>>
>>> greeting = "Hi, welcome to Real Python!"
>>> "Hi" in greeting
True
>>> "Hi" not in greeting
False
>>> "Hello" in greeting
False
>>> "Hello" not in greeting
True
For the string data type, an expression like substring in string
is True
if substring
is part of string
. Otherwise, the expression is False
.
An important point to remember when using membership tests on strings is that string comparisons are case-sensitive:
>>>
>>> "PYTHON" in greeting
False
This membership test returns False
because strings comparisons are case-sensitive, and "PYTHON"
in uppercase isn’t present in greeting
. To work around this case sensitivity, you can normalize all your strings using either the .upper()
or .lower()
method:
>>>
>>> "PYTHON".lower() in greeting.lower()
True
In this example, you use .lower()
to convert the target substring and the original string into lowercase letters. This conversion tricks the case sensitivity in the implicit string comparison.
Generators
Generator functions and generator expressions create memory-efficient iterators known as generator iterators. To be memory efficient, these iterators yield items on demand without keeping a complete series of values in memory.
In practice, a generator function is a function that uses the yield
statement in its body. For example, say that you need a generator function that takes a list of numbers and returns an iterator that yields square values from the original data. In this case, you can do something like this:
>>>
>>> def squares_of(values):
... for value in values:
... yield value ** 2
...
>>> squares = squares_of([1, 2, 3, 4])
>>> next(squares)
1
>>> next(squares)
4
>>> next(squares)
9
>>> next(squares)
16
>>> next(squares)
Traceback (most recent call last):
...
StopIteration
This function returns a generator iterator that yields square numbers on demand. You can use the built-in next()
function to retrieve consecutive values from the iterator. When the generator iterator is completely consumed, it raises a StopIteration
exception to communicate that no more values are left.
You can use the membership operators on a generator function like squares_of()
:
>>>
>>> 4 in squares_of([1, 2, 3, 4])
True
>>> 9 in squares_of([1, 2, 3, 4])
True
>>> 5 in squares_of([1, 2, 3, 4])
False
The in
operator works as expected when you use it with generator iterators, returning True
if the value is present in the iterator and False
otherwise.
However, there’s something you need to be aware of when checking for membership on generators. A generator iterator will yield each item only once. If you consume all the items, then the iterator will be exhausted, and you won’t be able to iterate over it again. If you consume only some items from a generator iterator, then you can iterate over the remaining items only.
When you use in
or not in
on a generator iterator, the operator will consume it while searching for the target value. If the value is present, then the operator will consume all the values up to the target value. The rest of the values will still be available in the generator iterator:
>>>
>>> squares = squares_of([1, 2, 3, 4])
>>> 4 in squares
True
>>> next(squares)
9
>>> next(squares)
16
>>> next(squares)
Traceback (most recent call last):
...
StopIteration
In this example, 4
is in the generator iterator because it’s the square of 2
. Therefore, in
returns True
. When you use next()
to retrieve a value from square
, you get 9
, which is the square of 3
. This result confirms that you no longer have access to the first two values. You can continue calling next()
until you get a StopIteration
exception when the generator iterator is exhausted.
Likewise, if the value isn’t present in the generator iterator, then the operator will consume the iterator completely, and you won’t have access to any of its values:
>>>
>>> squares = squares_of([1, 2, 3, 4])
>>> 5 in squares
False
>>> next(squares)
Traceback (most recent call last):
...
StopIteration
In this example, the in
operator consumes squares
completely, returning False
because the target value isn’t in the input data. Because the generator iterator is now exhausted, a call to next()
with squares
as an argument raises StopIteration
.
You can also create generator iterators using generator expressions. These expressions use the same syntax as list comprehensions but replace the square brackets ([]
) with round brackets (()
). You can use the in
and not in
operators with the result of a generator expression:
>>>
>>> squares = (value ** 2 for value in [1, 2, 3, 4])
>>> squares
<generator object <genexpr> at 0x1056f20a0>
>>> 4 in squares
True
>>> next(squares)
9
>>> next(squares)
16
>>> next(squares)
Traceback (most recent call last):
...
StopIteration
The squares
variable now holds the iterator that results from the generator expression. This iterator yields square values from the input list of numbers. Generator iterators from generator expressions work the same as generator iterators from generator functions. So, the same rules apply when you use them in membership tests.
Another critical issue can arise when you use the in
and not in
operators with generator iterators. This issue can appear when you’re working with infinite iterators. The function below returns an iterator that yields infinite integers:
>>>
>>> def infinite_integers():
... number = 0
... while True:
... yield number
... number += 1
...
>>> integers = infinite_integers()
>>> integers
<generator object infinite_integers at 0x1057e8c80>
>>> next(integers)
0
>>> next(integers)
1
>>> next(integers)
2
>>> next(integers)
3
>>> next(integers)
The infinite_integers()
function returns a generator iterator, which is stored in integers
. This iterator yields values on demand, but remember, there will be infinite values. Because of this, it won’t be a good idea to use the membership operators with this iterator. Why? Well, if the target value isn’t in the generator iterator, then you’ll run into an infinite loop that’ll make your execution hang.
Dictionaries and Sets
Python’s membership operators also work with dictionaries and sets. If you use the in
or not in
operators directly on a dictionary, then it’ll check whether the dictionary has a given key or not. You can also do this check using the .keys()
method, which is more explicit about your intentions.
You can also check if a given value or key-value pair is in a dictionary. To do these checks, you can use the .values()
and .items()
methods, respectively:
>>>
>>> likes = {"color": "blue", "fruit": "apple", "pet": "dog"}
>>> "fruit" in likes
True
>>> "hobby" in likes
False
>>> "blue" in likes
False
>>> "fruit" in likes.keys()
True
>>> "hobby" in likes.keys()
False
>>> "blue" in likes.keys()
False
>>> "dog" in likes.values()
True
>>> "drawing" in likes.values()
False
>>> ("color", "blue") in likes.items()
True
>>> ("hobby", "drawing") in likes.items()
False
In these examples, you use the in
operator directly on your likes
dictionary to check whether the "fruit"
, "hobby"
, and "blue"
keys are in the dictionary or not. Note that even though "blue"
is a value in likes
, the test returns False
because it only considers the keys.
Next up, you use the .keys()
method to get the same results. In this case, the explicit method name makes your intentions much clearer to other programmers reading your code.
To check if a value like "dog"
or "drawing"
is present in likes
, you use the .values()
method, which returns a view object with the values in the underlying dictionary. Similarly, to check if a key-value pair is contained in likes
, you use .items()
. Note that the target key-value pairs must be two-item tuples with the key and value in that order.
If you’re using sets, then the membership operators work as they would with lists or tuples:
>>>
>>> fruits = {"apple", "banana", "cherry", "orange"}
>>> "banana" in fruits
True
>>> "banana" not in fruits
False
>>> "grape" in fruits
False
>>> "grape" not in fruits
True
These examples show that you can also check whether a given value is contained in a set by using the membership operators in
and not in
.
Now that you know how the in
and not in
operators work with different built-in data types, it’s time to put these operators into action with a couple of examples.
Putting Python’s in
and not in
Operators Into Action
Membership tests with in
and not in
are pretty common operations in programming. You’ll find these kinds of tests in many existing Python codebases, and you’ll use them in your code as well.
In the following sections, you’ll learn how to replace Boolean expressions based on the or
operator with membership tests. Because membership tests can be quite common in your code, you’ll also learn how to make these tests more efficient.
Replacing Chained or
Operators
Using a membership test to replace a compound Boolean expression with several or
operators is a useful technique that allows you to simplify your code and make it more readable.
To see this technique in action, say that you need to write a function that takes a color name as a string and determines whether it’s a primary color. To figure this out, you’ll use the RGB (red, green, and blue) color model:
>>>
>>> def is_primary_color(color):
... color = color.lower()
... return color == "red" or color == "green" or color == "blue"
...
>>> is_primary_color("yellow")
False
>>> is_primary_color("green")
True
In is_primary_color()
, you use a compound Boolean expression that uses the or
operator to check if the input color is either red, green, or blue. Even though this function works as expected, the condition may be confusing and difficult to read and understand.
The good news is that you can replace the above condition with a compact and readable membership test:
>>>
>>> def is_primary_color(color):
... primary_colors = {"red", "green", "blue"}
... return color.lower() in primary_colors
...
>>> is_primary_color("yellow")
False
>>> is_primary_color("green")
True
Now your function uses the in
operator to check whether the input color is red, green, or blue. Assigning the set of primary colors to a properly named variable like primary_colors
also helps to make your code more readable. The final check is pretty clear now. Anyone reading your code will immediately understand that you’re trying to determine if the input color is a primary color according to the RGB color model.
If you look at the example again, then you’ll notice that the primary colors have been stored in a set. Why? You’ll find your answer in the following section.
Writing Efficient Membership Tests
Python uses a data structure called a hash table to implement dictionaries and sets. Hash tables have a remarkable property: looking for any given value in the data structure takes about the same time, no matter how many values the table has. Using Big O notation, you’ll say that value lookups in hash tables have a time complexity of O(1), which makes them super fast.
Now, what does this feature of hash tables have to do with membership tests on dictionaries and sets? Well, it turns out that the in
and not in
operators work very quickly when they operate on these types. This detail allows you to optimize your code’s performance by favoring dictionaries and sets over lists and other sequences in membership tests.
To have an idea of how much more efficient than a list a set can be, go ahead and create the following script:
# performance.py
from timeit import timeit
a_list = list(range(100_000))
a_set = set(range(100_000))
list_time = timeit("-1 in a_list", number=1, globals=globals())
set_time = timeit("-1 in a_set", number=1, globals=globals())
print(f"Sets are {(list_time / set_time):.2f} times faster than Lists")
This script creates a list of integer numbers with one hundred thousand values and a set with the same number of elements. Then the script computes the time that it takes to determine if the number -1
is in the list and the set. You know up front that -1
doesn’t appear in the list or set. So, the membership operator will have to check all the values before getting a final result.
As you already know, when the in
operator searches for a value in a list, it uses an algorithm with a time complexity of O(n). On the other hand, when the in
operator searches for a value in a set, it uses the hash table lookup algorithm, which has a time complexity of O(1). This fact can make a big difference in terms of performance.
Go ahead and run your script from the command line using the following command:
$ python performance.py
Sets are 1563.33 times faster than Lists
Although your command’s output may be slightly different, it’ll still show a significant performance difference when you use a set instead of a list in this specific membership test. With a list, the processing time will be proportional to the number of values. With a set, the time will be pretty much the same for any number of values.
This performance test shows that when your code is doing membership checks on large collections of values, you should use sets instead of lists whenever possible. You’ll also benefit from sets when your code performs several membership tests during its execution.
However, note that it’s not a good idea to convert an existing list into a set just to perform a few membership tests. Remember that converting a list into a set is an operation with O(n) time complexity.
Using operator.contains()
for Membership Tests
The in
operator has an equivalent function in the operator
module, which comes in the standard library. The function is called contains()
. It takes two arguments—a collection of values and a target value. It returns True
if the input collection contains the target value:
>>>
>>> from operator import contains
>>> contains([2, 3, 5, 9, 7], 5)
True
>>> contains([2, 3, 5, 9, 7], 8)
False
The first argument to contains()
is the collection of values, and the second argument is the target value. Note that the order of arguments differs from a regular membership operation, where the target value comes first.
This function comes in handy when you’re using tools like map()
, or filter()
to process iterables in your code. For example, say you have a bunch of Cartesian points stored as tuples inside a list. You want to create a new list containing only the points that aren’t over the coordinate axis. Using the filter()
function, you can come up with the following solution:
>>>
>>> points = [
... (1, 3),
... (5, 0),
... (3, 7),
... (0, 6),
... (8, 3),
... (2, 0),
... ]
>>> list(filter(lambda point: not contains(point, 0), points))
[(1, 3), (3, 7), (8, 3)]
In this example, you use filter()
to retrieve the points that don’t contain a 0
coordinate. To do this, you use contains()
in a lambda
function. Because filter()
returns an iterator, you wrap up everything in a call to list()
to convert the iterator into a list of points.
Even though the construct in the above example works, it’s quite complex because it implies importing contains()
, creating a lambda
function on top of it, and calling a couple of functions. You can get the same result using a list comprehension either with contains()
or the not in
operator directly:
>>>
>>> [point for point in points if not contains(point, 0)]
[(1, 3), (3, 7), (8, 3)]
>>> [point for point in points if 0 not in point]
[(1, 3), (3, 7), (8, 3)]
The above list comprehensions are shorter and arguably more readable than the equivalent filter()
call from the previous example. They’re also less complex because you don’t need to create a lambda
function or call list()
, so you’re reducing the knowledge requirements.
Supporting Membership Tests in User-Defined Classes
Providing a .__contains__()
method is the most explicit and preferred way to support membership tests in your own classes. Python will automatically call this special method when you use an instance of your class as the right operand in a membership test.
You’ll likely add a .__contains__()
method only to classes that’ll work as collections of values. That way, the users of your class will be able to determine if a given value is stored in a specific instance of your class.
As an example, say that you need to create a minimal stack data structure to store values following the LIFO (last in, first out) principle. One requirement of your custom data structure is to support membership tests. So, you end up writing the following class:
# stack.py
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def __contains__(self, item):
return item in self.items
Your Stack
class supports the two core functionalities of stack data structures. You can push a value to the top of the stack and pop a value from the top of the stack. Note that your data structure uses a list
object under the hood to store and manipulate the actual data.
Your class also supports membership tests with the in
and not in
operators. To do this, the class implements a .__contains__()
method that relies on the in
operator itself.
To try out your class, go ahead and run the following code:
>>>
>>> from stack import Stack
>>> stack = Stack()
>>> stack.push(1)
>>> stack.push(2)
>>> stack.push(3)
>>> 2 in stack
True
>>> 42 in stack
False
>>> 42 not in stack
True
Your class fully supports the in
and not in
operators. Great job! You now know how to support membership tests in your own classes.
Note that if a given class has a .__contains__()
method, then the class doesn’t have to be iterable for the membership operators to work. In the example above, Stack
isn’t iterable, and the operators still work because they retrieve their result from the .__contains__()
method.
There are at least two more ways to support membership tests in user-defined classes apart from providing a .__contains__()
method. If your class has either an .__iter__()
or a .__getitem__()
method, then the in
and not in
operators also work.
Consider the following alternative version of Stack
:
# stack.py
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def __iter__(self):
yield from self.items
The .__iter__()
special method makes your class iterable, which is enough for membership tests to work. Go ahead and give it a try!
Another way to support membership tests is to implement a .__getitem__()
method that handles indexing operations using zero-based integer indices in your classes:
# stack.py
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def __getitem__(self, index):
return self.items[index]
Python automatically calls the .__getitem__()
method when you perform indexing operations on the underlying object. In this example, when you do stack[0]
, you’ll get the first item in the Stack
instance. Python takes advantage of .__getitem__()
to make the membership operators work correctly.
Conclusion
Now you know how to perform membership tests using Python’s in
and not in
operators. This type of test allows you to check if a given value is present in a collection of values, which is a pretty common operation in programming.
In this tutorial, you’ve learned how to:
- Run membership tests using Python’s
in
andnot in
operators - Use the
in
andnot in
operators with different data types - Work with
operator.contains()
, the equivalent function to thein
operator - Support
in
andnot in
in your own classes
With this knowledge, you’re good to go with membership tests using Python’s in
and not in
operators in your code.
In this tutorial, you’ll learn how to use Python to check if a list contains an item. Put differently, you’ll learn if an item exists in a Python list. Being able to determine if a Python list contains a particular item is an important skill when you’re putting together conditional expressions. For example, if you’re developing a web application, you may want to see if a user has already selected a number of values. Otherwise, if you’re developing a game, you may want to see if a user has different items in their inventory.
By the end of having read this tutorial, you’ll have learned how to check for membership in a list. You’ll learn how to do this using the in
keyword, as well as checking if an item doesn’t exist in a Python list using the not in
keywords. You’ll also learned how to check if a list contain an item using the Python any()
function, as well as the count()
function. Finally, you’ll learn some naive implementations of checking membership in a list, such as for-loops and list comprehensions.
The Quick Answer: Use in
To Check if a Python List Contains an Item
One of the easiest and most Pythonic ways to check for membership in a Python list is to use the in
key. Technically, the in
keyword serves two purposes:
- To check for membership in a list, and
- To loop over a items in a for loop
In this case, we’ll use the in
keyword to check if an item exists in a list. This provides a readable and almost plain-English way to check for membership. Let’s see what this looks like:
# Check if a Python List Contains an Item
items = ['datagy', 'apples', 'bananas']
if 'datagy' in items:
print('Item exists!')
# Returns: Item exists
We can that by writing if 'item' in items
, we’re able to evaluate if something exists in our list. This is a really intuitive way to check if items exist or not.
In the next section, you’ll learn how to use Python to check if an item doesn’t exist.
Need to replace an item in a list? Check out my tutorial on this topic here: Python: Replace Item in List (6 Different Ways)
Check if a Python List Doesn’t Contain an Item Using not in
In this section, you’ll learn to check if a list doesn’t contain an item. We can do this by negating the in
keyword, using the not
keyword. Similar to the example above, this reads like relatively plain English. Let’s see how we can use the not in
keyword to see if an item isn’t a list:
# Check if a Python List Doesn't Contain an Item
items = ['datagy', 'apples', 'bananas']
if 'oranges' not in items:
print("Item doesn't exists!")
# Returns: Item doesn't exist!
We can see that the not in
keyword allows us to determine if an item doesn’t exist. It returns the opposite truthiness of the in
keyword. Because of this, it allows us to write cleaner code.
In the next section, you’ll learn to check for membership in a Python list using the .count()
method.
Need to remove an item for a list? Check out my tutorial to learn how to here: Remove an Item from a Python List (pop, remove, del, clear)
Check if a Python List Contains an Item Using count
Python lists come with a number of different helpful methods. One of these methods is the .count()
method, which counts the number of times an item appears in a list. Because of this, we can see if an item exists in a list if the count of that item is anything greater than 0. If the count is 0, then the item doesn’t exist in our list.
Let’s see what this looks like:
# Check if a Python List Contains an Item using .count()
items = ['datagy', 'apples', 'bananas']
if items.count('datagy') > 0:
print('Item exists!')
# Returns: Item exists!
If any item exists, the count will always be greater than 0. If it doesn’t, the count will always be 0.
We can combine this if statement into a single line as well. Let’s see what this looks like:
# Check if a Python List Contains an Item using .count()
items = ['datagy', 'apples', 'bananas']
print('Exists') if items.count('datagy') else print("Doesn't exist")
# Returns: Exists
The expression here works like the Python ternary operator. If you want to learn more about how the Python ternary operator works, check out my in-depth guide on the ternary operator here.
In the next section, you’ll learn how to use the any()
function to check for membership in a Python list.
Want to learn how to shuffle a list? Check out my tutorial here: Python: Shuffle a List (Randomize Python List Elements)
Check if a Python List Contains an Item Using any
The Python any
function scans an iterable and returns True
if any of the items in the iterable are True
. So, how can we use the any
function to check for membership in a Python list? We can place a comprehension inside the any
function to check for membership. If any item is returned, the any
function will return True
.
Let’s see what this looks like and then dive into how this works:
# Check if a Python List Contains an Item using any()
items = ['datagy', 'apples', 'bananas']
print(any(item=='datagy' for item in items))
# Returns: True
The way that this works is that the comprehension will loop over each item in the list and check if the item is equal to the one we want to check for. If it is, the comprehension returns True
for that item. Otherwise, it’ll return False
. Because of this, if a True
value exists, the any
function will return True
.
In the next section, you’ll learn how to use a for loop to check if an item exists in a list.
Want to learn how to check the length of a Python list? Learn all you need to know with this tutorial that covers five different ways to check the length: Python List Length or Size: 5 Ways to Get Length of List
Check if a Python List Contains an Item Using a For Loop
In this final section, you’ll learn how to use a for loop to check for membership in a list. We can loop over each item in our list and see if the item matches we want to check. Once the item is found, the for loop sets a value to True
and the for loop breaks.
Let’s see what this looks like in Python:
# Check if a Python List Contains an Item using a for loop
items = ['datagy', 'apples', 'bananas']
exists = False
for item in items:
if item == 'datagy':
exists = True
break
print(exists)
# Returns: True
Let’s break down what we did here:
- We created our list and a variable,
exists
, which is set toFalse
by default - We loop over every item in the list and see if it equal to the value we want to check membership for
- If the item is equal to the one we want to check for, then we set
exists
toTrue
and break the for loop
Need to remove duplicate items from a Python list? This tutorial will teach you seven different ways to do this: Python: Remove Duplicates From a List (7 Ways)
Conclusion
In this tutorial, you learned how to check for membership in a Python list, meaning checking whether an item exists. You learned how to do this using the in
keyword. You then learned how to check whether an item doesn’t exist, using the not in
keyword. You then learned other methods to check for membership, using the any
function, the .count
method, and a Python for loop.
To learn more about the Python in
keyword, check out the official documentation here. The official documentation for the any
function can be found here and for the .count
method, it can be found here.
Introduction
In this tutorial, we’ll take a look at how to check if a list contains an element or value in Python. We’ll use a list of strings, containing a few animals:
animals = ['Dog', 'Cat', 'Bird', 'Fish']
Check if List Contains Element With for Loop
A simple and rudimentary method to check if a list contains an element is looping through it, and checking if the item we’re on matches the one we’re looking for. Let’s use a for
loop for this:
for animal in animals:
if animal == 'Bird':
print('Chirp!')
This code will result in:
Chirp!
Check if List Contains Element With in Operator
Now, a more succinct approach would be to use the built-in in
operator, but with the if
statement instead of the for
statement. When paired with if
, it returns True
if an element exists in a sequence or not. The syntax of the in
operator looks like this:
element in list
Making use of this operator, we can shorten our previous code into a single statement:
if 'Bird' in animals: print('Chirp')
This code fragment will output the following:
Chirp
This approach has the same efficiency as the for
loop, since the in
operator, used like this, calls the list.__contains__
function, which inherently loops through the list — though, it’s much more readable.
Check if List Contains Element With not in Operator
By contrast, we can use the not in
operator, which is the logical opposite of the in
operator. It returns True
if the element is not present in a sequence.
Let’s rewrite the previous code example to utilize the not in
operator:
if 'Bird' not in animals: print('Chirp')
Running this code won’t produce anything, since the Bird
is present in our list.
But if we try it out with a Wolf
:
if 'Wolf' not in animals: print('Howl')
This code results in:
Howl
Check if List Contains Element With Lambda
Another way you can check if an element is present is to filter out everything other than that element, just like sifting through sand and checking if there are any shells left in the end. The built-in filter()
method accepts a lambda function and a list as its arguments. We can use a lambda function here to check for our 'Bird'
string in the animals
list.
Then, we wrap the results in a list()
since the filter()
method returns a filter
object, not the results. If we pack the filter
object in a list, it’ll contain the elements left after filtering:
retrieved_elements = list(filter(lambda x: 'Bird' in x, animals))
print(retrieved_elements)
This code results in:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
['Bird']
Now, this approach isn’t the most efficient. It’s fairly slower than the previous three approaches we’ve used. The filter()
method itself is equivalent to the generator function:
(item for item in iterable if function(item))
The slowed down performance of this code, amongst other things, comes from the fact that we’re converting the results into a list in the end, as well as executing a function on the item on each iteration.
Check if List Contains Element Using any()
Another great built-in approach is to use the any()
function, which is just a helper function that checks if there are any (at least 1) instances of an element in a list. It returns True
or False
based on the presence or lack thereof of an element:
if any(element in 'Bird' for element in animals):
print('Chirp')
Since this results in True
, our print()
statement is called:
Chirp
This approach is also an efficient way to check for the presence of an element. It’s as efficient as the first three.
Check if List Contains Element Using count()
Finally, we can use the count()
function to check if an element is present or not:
list.count(element)
This function returns the occurrence of the given element in a sequence. If it’s greater than 0, we can be assured a given item is in the list.
Let’s check the results of the count()
function:
if animals.count('Bird') > 0:
print("Chirp")
The count()
function inherently loops the list to check for the number of occurrences, and this code results in:
Chirp
Conclusion
In this tutorial, we’ve gone over several ways to check if an element is present in a list or not. We’ve used the for
loop, in
and not in
operators, as well as the filter()
, any()
and count()
methods.
There are the following methods to check if a list contains an element in Python.
- Method 1: Using the “in” operator
- Method 2: Using list comprehension
- Method 3: Using a list.count() method
- Method 4: Using any() function
- Method 5: Using the “not in” operator
Method 1: Using the “in” operator
To check if the list contains an element in Python, use the “in” operator. The “in” operator checks whether the list contains a specific item. It can also check if the element exists on the list using the list.count() function.
This approach returns True if an item exists in the list and False if an item does not exist. The list need not be sorted to practice this approach of checking.
Checking if the item exists in the list
To check if an item exists in the list, use the “in operator”. Then, use the “in” operator with the if condition, and if the item exists in the list, then the condition returns True, and if not, it returns False.
Syntax
Return Value
It will return True if an item exists in the list else returns False.
Example
listA = ['Stranger Things', 'S Education', 'Game of Thrones']
if 'S Eductation' in listA:
print("'S Eductation' found in List : ", listA)
Output
'S Eductation' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']
Let’s take an example where we do not find an item on the list.
listA = ['Stranger Things', 'S Education', 'Game of Thrones']
if 'Dark' in listA:
print("Yes, 'S Eductation' found in List : ", listA)
else:
print("Nope, 'Dark' not found in the list")
Output
Nope, 'Dark' Not found in the list
The list does not contain the dark element, so it returns False, and the else block executes.
Method 2: Using List comprehension
Use a list comprehension to check if a list contains single or multiple elements in Python.
main_list = [11, 21, 19, 18, 46]
check_elements = [11, 19, 46]
if all([item in main_list for item in check_elements]):
print("The list contains all the items")
Output
The list contains all the items
In this example, we defined two lists.
- The main_list is our primary list which we will check for multiple elements.
- The check_elements list contains elements we will check in the main_list.
Using list comprehension, we check if all the elements of check_list are present in the main_list.
In our case, It returns True since all the elements are present. That’s why if condition is executed.
Method 3: Using the list.count() function
To check if the item exists in the Python list, use the list.count() method.
Syntax
The List count(item) method returns the occurrence count of the given element. If it’s greater than 0, a given item exists in the list.
listA = ['Stranger Things', 'S Education', 'Game of Thrones']
if listA.count('Stranger Things') > 0:
print("'Stranger Things' found in List : ", listA)
Output
'Stranger Things' found in List : ['Stranger Things', 'S Education', 'Game of Thrones']
Method 4: Using any() method
Python any() function is the most classical way to perform this task efficiently. The any() function checks for a match in a string with a match of each list element.
data_string = "The last season of Game of Thrones was not good"
listA = ['Stranger Things', 'S Education', 'Game of Thrones']
print("The original string : " + data_string)
print("The original list : " + str(listA))
res = any(item in data_string for item in listA)
print("Does string contain 'Game of Thrones' list element: " + str(res))
Output
The original string : The last season of Game of Thrones was not good
The original list : ['Stranger Things', 'S Education', 'Game of Thrones']
Does string contain 'Game of Thrones' list element: True
From the output, Game of Thrones exists in the list.
Method 5: Use not in inverse operator.
Python “not in” is a built-in operator that evaluates to True if it does not find a variable in the specified sequence and False otherwise.
To check if the list contains a particular item, you can use the “not in” inverse operator.
listA = ['Stranger Things', 'S Education', 'Game of Thrones']
if 'Witcher' not in listA:
print("'Witcher' is not found in List : ", listA)
Output
'Witcher' is not found in List : ['Stranger Things', 'S Education', 'Game of Thrones']
Python in and not in operators work fine for lists, tuples, sets, and dicts (check keys).
Conclusion
The best and most efficient way to check if a list contains an element is to use the “in operator” in Python.