How to check if an element is in a list python?

A list is a built-in data type that is used to store multiple items of different data types. This article discusses the different approaches to check if an element is present in the list.

Checking the existence of an element using in operator

In python, the in operator can be used to check the presence of a value in a sequence(string, list, tuple, set, dictionary). It returns a boolean value 'True' if the item is found in the sequence else it returns False.

Let us consider a list my_list = ["bat", 3, "apple", "dog", 4.8 ]. To check if element "cat" is present in the my_list we use in operator. If the item is found in my_list the program prints "Item found" else it prints "Item not found".

my_list = ["bat", 3, "apple", "dog", 4.8 ]
print("my_list = ", my_list)
if "cat" in my_list:
    print("Item found")
else:
    print("Item not found")

The output for the above program is "Item not found".

my_list =  ['bat', 3, 'apple', 'dog', 4.8]
Item not found

Checking the existence of an element using the count() method

count() method is used to count the number of occurrences of an item. In other words, it searches for the item in a given sequence(string, list, tuple, set, dictionary) and returns an integer value indicating the number of times the item is present in the sequence.

The syntax for count() method is

#syntax:
sequence.count(item)

Let us consider a list my_list = ["bat", 3, "apple", "dog", 4.8 ]. We will use count() method to check if 3 is present in list.If count() returns a value greater than '0' then item is present in the list else the item doesn't exists.

my_list = ["bat", 3, "apple", "dog", 4.8 ]
print("my_list = ", my_list)
item_count = my_list.count(3)
if (item_count > 0):
    print("Element 3 is present in my_list")
else:
    print("Element 3 is not present in my_list")

The above program gives output as Element 3 is present in my_list.

my_list =  ['bat', 3, 'apple', 'dog', 4.8]
Element 3 is present in my_list

Checking the existence of an element using for loop

Let us consider a list my_list = ["bat", 3, "apple", "dog", 4.8 ]. To check an element for eg. apple is present in the my_list we use a for loop to iterate over the list. We also use a flag variable that is initialized to 0.

In each iteration, the item in my_list is compared with the value apple. If the item is found flag is set to 1 and the program prints "Item found" and breaks the loop. If the item is not found in the list the program prints "Not found".

my_list = ["bat", 3, "apple", "dog", 4.8 ]
print("my_list = ", my_list)
flag = 0
for item in my_list:
    if (item == "apple"):
        flag = 1
        print("Item found")
        break
if flag == 0:
    print("Not found")

The output for the above program is "Item found".

my_list =  ['bat', 3, 'apple', 'dog', 4.8]
Item found

Checking the existence of an element using the set() + in operator

In this method, a list is converted to a set(), and in operator is used to check the existence of the specified element. The advantage of this method is average time complexity to search an element in a set is O(1).

The drawback that comes with this method is the order of the list is not preserved and duplicate elements are removed. Also, if we create a new_list to store the original list it will take extra space.

Example,

my_list = ["bat", 3, "apple", "dog", 4.8 ]
my_list = set(my_list)
print("my_list", my_list)
if "apple" in my_list:
    print("Item Found")
else:
    print("Item not found")

The above program gives output as "Item found".

my_list {3, 4.8, 'bat', 'apple', 'dog'}
Item Found

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions