Python : How to Check if an item exists in list ?

How to Check if an item exists in list using Python?

In Python List is a most versatile datatype which stores multiple items in a single variable. All the items present inside a list are indexed where the first item is associated with the index position 0 (Zero). In this article we will discuss how to check whether the list contains a specific item or not. Let’s explore the concept in more detail.

Example of a List :

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]

So, our task is to check an item that is present in the list or not. Suppose we are looking for the item ‘Mumbai’ in List_item and it is present in the list but if we will look for the item ‘Goa’ that does not exist in the list named List_item.

There are multiple approaches available which can be used to achieve this. So, let’s discuss each approach one by one.

Method-1 : Checking if the item exists in the list using “in” operator

"in" operator in python can be used to check the item exists in the list or not. With the help of if condition it can check whether the item is present or not. If it is present in the list then it returns True and if it is not present in the list it returns False.

Syntax : item_name in list_name
#Program :

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]
#Checking an item which is present in the list
if 'Mumbai' in List_item:
    print('Item Mumbai is present')
else:
    print('Item Mumbai is not present')
#Checking an item which is not present in the list
if 'Goa' in List_item:
    print('Item Goa is present')
else:
    print('Item Goa is not present')
Output :
Item Mumbai is present
Item Goa is not present

Method-2 : Checking if the item exists in list using list.count() function

list.count() method gives the occurrence of an item in the list. Means if the item found at least once or more than that then the item is present if the item occurrence is zero then that item is not present in the list.

Synatx :  item_name.count(item)
#Program :

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]
#Checking an item which is present in the list
#Occurrence of Chennai is 1
if List_item.count('Chennai'):
    print('Item Chennai is present')
else:
    print('Item Chennai is not present')
#Checking an item which is not present in the list
#Occurrence of Pune is 1
if List_item.count('Pune'):
    print('Item Pune is present')
else:
    print('Item Pune is not present')
Output :
Item Chennai is present
Item Pune is not present

Method-3 : Checking if the item exists in list using any() function

Using any( ) function for checking a string is a most classical way of performing this task. With the help of any( ) function we can check if any item of given iterable is True or we can check for a match in a string with a match of each list item/element.

Syntax : any(condition)
Python :

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ]

#Checking an item which is present in the list
#Checking any item whose length is 11 exist in the list or not
result = any(len(item) == 11 for item in List_item)
if result:
    print('Item Bhubaneswar is present')
else:
    print('Item bhubaneswar is not present')

#Checking an item which is not present in the list
#Checking any item whose length is 1 exist in the list or not
result = any(len(item) == 1 for item in List_item)
if result:
    print('Item found with length 1')
else:
    print('No Item found with length 1')
Output :
Item Bhubaneswar is present
No Item found with length 1

Method-4 : Checking if the item exists in list using ‘not in’ inverse operator

'not in' inverse operator in python is an inbuilt operator which returns True if the item is not present in the list and it returns False if the item is present in the list.

Syntax : item_name not in list_name
Program :

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ] 
#Checking an item which is not present in the list
#It will result true because Goa is not present in list
if 'Goa' not in List_item: 
    print('True, Goa not in list') 
else :
    print('False, Goa is in list') 

#Checking an item which is present in the list 
#It will result false because Delhi is present in list
if 'Delhi' not in List_item: 
    print('True, Delhi not in list') 
else :
    print('False, Delhi is in list')
Output :
True, Goa not in list
False, Delhi is in list

Method-5 : Checking if the item exists in list using Naive method

The naive method is the most simplest way of checking the existance of an element as it iterates the through all the elements of the list. If the item found in the list then it return true.

#Program :

List_item = [ 'Bhubaneswar' , 'Mumbai' , 'Kolkatta' , 'Chennai' , 'Delhi' ] 
#Checking an item which is not present in the list
#It will result true because Kolkatta present in list

for i in List_item: 
    if i == 'Kolkatta':
        print("True")
Output :
True