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.
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.
If You’re in Hurry…
You can use the If in
statement below to check if a value exists in a list.
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')
If the item is present in the list, the statement next to IF will be executed; if it doesn’t exist, the else part will be executed.
If You Want to Understand Details, Read on…
Different methods are available to check if a value exists in a list. Let us look at each one in detail.
Table of Contents
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 code below demonstrates checking 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.
It counts the number of times the passed item exists in a list. Use the if
statement and check if the count is greater than 0
. This means the items exist at least once in the list.
The String comparison is case sensitive in the count()
method.
Code
The code below demonstrates how to check the count of a String to check if that String exists in the list.
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()
You can also use the any() method to check if an item is present in the list.
The any()
method checks if any of the items in the list is True
.
To check if an item exists in the list, create an array of True
and False
values using the list comprehension
.
True
– For matching itemsFalse
– For unmatching items
This method is case sensitive. You can convert both the string to a single case to make a case insensitive check. either upper()
or lower()
.
Then, the any()
method will return True
if at least one True
in the list.
Code
The code below demonstrates how to check if a string
exists in the list using the any()
method.
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
Since the item Orange
is present in the list, you’ll see the output True
.
Is the item present in the list? :- True
Using Set() Method
This section demonstrates how to convert the list
to set
before checking if the value exists in the list.
Once the list is converted to set
, you can again use the if in
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
The code below demonstrates how to convert the list
to set
and check if an item is available in the set
.
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.
Conclusion
You’ve learned how to check if an item exists in a list using different methods. You’ve also learned when it is appropriate to use each technique.
Also, the fastest method to check if a value exists in the list is using if in
.
You May Also Like
- How to count the number of elements in the list
- How To Get Unique Values From a List in Python
- How To Concatenate Lists in Python
- How to check if a list is empty in Python
- How to Remove an element from a List Using Index in Python
- How To Get The Last Element Of A List In Python
- How To Remove Duplicates From the List In Python