Check if a value exists in a list in Python
This post will discuss how to check if a value exists in a Python List.
1. Using In operator
The standard way to find a value in a Python list is with the in operator. This operator can test whether an object is contained within another object, such as a value within a list. This is demonstrated below:
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 5] item = 3 isPresent = item in ints print(isPresent) # True |
The code above resulted in True because 3 is an element of the list. Note that the in operator internally calls the __contains__() function:
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 5] item = 3 isPresent = ints.__contains__(item) print(isPresent) # True |
This method is easy to use and works for any type of value. It works by scanning through all the elements in the list until it finds the value or end of the list is reached.
2. Using Set
The in operator is no doubt the simplest and the most elegant way to find a value in a list. But if you need to perform multiple lookups, consider converting the list to a set. As lookups in the set are very efficient, you can do this in constant time.
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 5] item = 3 isPresent = item in set(ints) print(isPresent) # True |
3. Using Dictionary
You can also create a dictionary of value-index pairs for doing multiple lookups. This is often useful when you need to find the index of the item as well.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
ints = [1, 2, 3, 4, 5] item = 3 indexMap = dict((x, i) for i, x in enumerate(ints)) try: index = indexMap[item] print(f'Item {item} is found at index {index}') except KeyError: print('Item not found') ''' Output: Item 3 is found at index 2 ''' |
Alternatively, you can use the get(item, default) function, which returns the item’s index if present; default value otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ints = [1, 2, 3, 4, 5] item = 3 indexMap = dict((x, i) for i, x in enumerate(ints)) index = indexMap.get(item, -1) if index != -1: print(f'Item {item} is found at index {index}') else: print('Item not found') ''' Output: Item 3 is found at index 2 ''' |
4. Using count() function
If you want to know the number times a value occurs within a list, you can use the count() function. It can also be used to check if a value exists in a Python list, as follows:
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 5] item = 3 isPresent = ints.count(item) > 0 print(isPresent) # True |
5. Using any() function
The any() function checks whether any element of an iterable (such as a list) is True or not. You can use it to check if a value exists in a Python list using a generator expression that iterates through each element of the list and compares it with 3 using the == operator. For example:
|
1 2 3 4 5 |
ints = [1, 2, 3, 4, 5] item = 3 isPresent = any(e == item for e in ints) print(isPresent) # True |
That’s all about determining whether a value exists in a list in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)