Python lists are used to store a list of values.
You can check if a value exists in a list using the if item in list
statement.
Basic Example
colours = ['Orange', 'White', 'Green', 'Blue']
if 'Orange' in colours:
print('Item is present in Colours List')
else:
print('Item is NOT present in Colours List')
In this tutorial, you’ll learn how to check if a value exists in a list in Python and which is the fastest method among the available methods.
Using IN operator
This section teaches you how to use the if in statement to check if an item exists in the list.
When you check for the String
existence, it checks in a case sensitive manner.
The processing time is also measured to see how long it takes to use the if in
statement to check if an item is available in the list. You’ll see the explanation for the performance in the last section.
Code
The following code demonstrates how to check if the String
Orange is available in the colours
list.
import time
colours = ['Orange', 'White', 'Green', 'Blue']
t = time.process_time()
if 'Orange' in colours:
print('Item is present in Colours List')
else:
print('Item is NOT present in Colours List')
elapsed_time = time.process_time() - t
print(elapsed_time)
Output
The below output is shown since the item Orange
exists in the list. Also, the elapsed time to check is displayed.
Item is present in Colours List
0.0005950000000000122
Using List.count()
You can also use the count() method to check if a value exists in a list.
- Counts the number of times the passed item exists in a list
- Use the
if
statement and check if the count is greater than0
. This means the items exist at least once in the list.
The String comparison is case-sensitive in the count()
method.
Code
colours = ['Orange', 'White', 'Green', 'Blue']
if colours.count('Orange') > 0:
print('Item is present in Colours List')
Output
The item Orange exists in the list. Hence you’ll see the below output.
Item is present in Colours List.
Using Any()
The any()
method checks if any of the items in the list is True
.
You can also use the any() method to check if an item is present in the list.
To check if an item exists in the list,
- Create an array of
True
andFalse
values using thelist comprehension
True
– For matching itemsFalse
– For unmatching items
This method is case-sensitive. You can convert both strings to a single case to make a case-insensitive check. either upper()
or lower()
.
The any()
method will return True
if at least one True
in the list.
Code
colours = ['Orange', 'White', 'Green', 'Blue']
result = any(item for item in colours if item =='Orange')
print(" Is the item present in the list? :- " +str(bool(result)))
Output
Is the item present in the list? :- True
Using Set() Method
This section demonstrates how to convert the list
to a set
before checking if the value exists in the list.
- Convert the
list
toset
- Use the
if in
a statement to check if the item exists in the list
The advantage of using this method is that all the duplicate items in the list
will be ignored while converting the’ list’ to’ set’. The set
contains only the unique values. Hence, the operation cost is O(1)
, whereas the operation cost for checking directly in the list
is O(n)
.
Use this method ONLY when the list
is huge. Otherwise, converting the list
to set
takes additional time.
Code
import time
colours = ['Orange', 'White', 'Green', 'Blue']
colours_set = set(colours)
t = time.process_time()
if 'Orange' in colours_set:
print('Item is present in Colours List')
else:
print('Item is NOT present in Colours List')
elapsed_time = time.process_time() - t
print(elapsed_time)
Output
Item is present in Colours List
0.0009959999999999969
Fastest Method To check If Value Exists in List
Based on the elapsed times displayed in the two sections, you can see that there is no significant difference in using the if in
method directly on the list
instead of using it on a set
.
Use the set
comparison when you need to check existence in a huge list.
Using Contains Function of Lists
There is no direct contains()
function in the list.
However, the in
operator internally calls the _contains_function of the list object.
For more details, read this StackOverflow answer.