Python list contains: How to check if an item exists in list?
hrishikesh1990
Posted on May 25, 2022
In Python, lists are important containers as they store all kinds of data types as a collection. It can contain up to 536,870,912 items in a 32-bit system. Sometimes it is difficult to know if the list contains a particular item. Python has various ways by which we can find out if the list contains the item.
Table of contents
- Python list contains - Introduction
- Check if the Python list contains an element using in operator
- Using for loop to check if the list contains an element in Python
- Using any() to check if the list contains an element
- count() to check if the list contains an element
- Closing thoughts
Python lists contain - Introduction
Checking if an element is present in a list is one of the basic list operations in Python and there are many different ways we can check that. In this tutorial, we will be covering some of the ways to check if the lists contain an element.
Check if the Python list contains an element using in operator
The most convenient way to check whether the list contains the element is using the in operator. Without sorting the list in any particular order, it returns TRUE if the element is there, otherwise FALSE.
The below example shows how this is done by using 'in' in the if-else statement.
Input:
list = [Adam, Dean, Harvey, Mick, John]
if 'John' in list:
print (" 'John' is found in the list")
else
print (" 'John' is not found in the list")
if 'Ned' in list:
print (" 'Ned' is found in the list")
else
print (" 'Ned' is not found in the list")
Output:
'John' is found in the list
'Ned' is not found in the list
Using for loop to check if the list contains an element in Python
Another simple method to check if the list contains the element is looping through it. As the name suggests, the loop will match each element of the list with the element that we are looking for one by one and will only stop if there's a match or there is no match at all. The below example illustrates this.
Input:
list = [Adam, Dean, Harvey, Mick, John]
for name in list:
if name == 'Adam':
print ("Found the element")
Output:
Found the element
Using any() to check if the list contains
The function any() is a built-in approach that checks for a match in a string with a match of each list element.
The below example shows how the any() function works. We check if there are any common items in the string, 'Adam lives in New York', and the list mentioned in the first line.
Input:
list = [Adam, Dean, Harvey, Mick, John]
string = "Adam lives in New York"
print ("The original list is: " + str(list))
print ("The original string is: " + string)
result = any(item in string for item in list)
print ("Does the string contain 'Adam': " + str(result))
Output:
The original list is: [Adam, Dean, Harvey, Mick, John]
The original string is: Adam lives in New York
Does the string contain 'Adam': True
count() to check if the list contains
Another built-in method in Python, count() returns the number of times the passed element occurs in the list. If the element is not there in the list then the count() will return 0. If it returns a positive integer greater than 0, it means the list contains the element.
Input:
list = [Adam, Dean, Harvey, Mick, John]
result = list.count(Harvey)
if result > 0:
print("Harvey exists in the list")
else:
print("Harvey does not exist in the list")
Output:
Harvey exists in the list
Closing thoughts
In this tutorial, we have used the 'in' operator, for loop, any() and count() methods to check whether a particular item exists in the list or not. One can learn more about other concepts related to Python here.
Posted on May 25, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.