How to check if an item exists in a list in Python

In this tutorial, we will learn how to check if an item exists in a list in Python.

As we know, a list is a collection of different elements.

Certainly, there would be a requirement to know whether an element is present in the list or not.

Sample List :

l = [ ] # empty list

l = [23, 45, 67, "hello", "jagannath"]

print (l)
Output :

[23, 45, 67, 'hello', 'jagannath']

Moreover, in the above code, a list contains elements namely ( 23, 45, etc )

So, our job is to know whether they are present in the list using different ways.

  1.  with “in” operator
  2.  using a For loop
  3.  dealing with the in-built method list.count( )

Using in operator: check if an item exists in a list in Python

The “in” operator checks whether or not the item exists in the list.

Moreover, it is used with the if statement.

l = [ ] # empty list

l = [23, 45, 67, "hello", "jagannath"]

if 23 in l:                             # in operator with if
    print ("Yes 23 is Present in the list")

if "pavan" in l:                            # there is no element "pavan" in the list
    print ("pavan is present in the list")
else:
    print ("pavan is not present in the list")
Output :

Yes 23 is Present in the list
pavan is not present in the list

Using a For loop: check if an item exists in a list in Python

This approach deals with a concept of iterating all elements in the list and checking every element sequentially.

Certainly, this approach is used commonly in all languages.

l = [ ] # empty list

l = [23, 45, 67, "hello", "jagannath"]

key = "hello"          # element to be searched

flag = 0              # initial value

for i in l:
    if i == key:             # checking every element
        flag=1
        print (key,"is present in list")
        break

if (flag == 0):             # only possible if i!=key upto all iterations
    print (key,"is not present in the list")
Output :

hello is present in list


Dealing with the method .count( ) :

The method list.count( item ) is used to get the number of times the item present in the list.

Therefore, this is more of a direct approach.

  • If the return value of the above method is 0, then the element is not present.
  • Else there is a 100% possibility that element is there in the list.
l = [ ] # empty list

l = [23, 45, 67, "hello", "jagannath"]

key1 = "hell"          # 1st element to be searched

key2 = "jagannath"     # 2nd element to be searched

count_1 = l.count(key1)  # count of key1 in the list

count_2 = l.count(key2)  # count of key2 in the list

if (count_1 > 0):
    print (key1,"is present in the list")          # output based on count value
else:
    print (key1,"is not present in the list")
    
if (count_2 > 0):
    print (key2,"is present in the list")
else:
    print (key2,"is not present in the list")
Output :

hell is not present in the list
jagannath is present in the list

 

Concluding, these are the following ways to check the existence of the item in a list.

Certainly, they are helpful while dealing with operations like linear search or binary search or some complex problems.

Leave a Reply

Your email address will not be published. Required fields are marked *