Online Tutorials & Training Materials | STechies.com
Register Login

Python: Check if Element Exists in List

04 Mar 2019 2:10 pm || 0

In python, list is a collections of data-types, which is used to store all the data types. In this tutorial we will learn in python, how to check if an item, element, number, value, object, word exists in the list?

How to Check in Python If an Item Exists in List?

1. Using “in” Operator

In this example, we are using ‘in’ operator to check if an item or element exists in a sequence or not. If an item exists in the list, it will return the output is true, else it returns false.

Example:

# Python3 code
# Check if element exists in the list
# Using in Operator

# Initialization of list
MyList = ['a','b','c','d','e']

# Print list
print("Our List: ", MyList)

# Check if 'b' exists in the list or not
if 'b' in MyList:
 print(" Item 'b' is present in the list")
else:
 Print(" Item 'b' is not present in the list")

Output:

Our List: ['a','b','c','d','e']
Item 'b' is present in the list

Execution Time: 0.0009 (Seconds)

Explanation:

In the above example, we used the ‘in’ operator to check whether ‘b’ exists in MyList or not. We used the if-else condition to print the result. Since ‘b’ is present in the list, the if block is executed. If ‘b’ was not present in MyList the else block would have been executed.

2. Using “not in” Operator

In this example, we are using a “not in” operator to check if an item or element exists in the list or not. If the element does not exist in the list it will return true else false.

Example:

# Python3 code
# Check if element exists in the list
# Using in Operator

# Initialization of list
MyList = ['a','b','c','d','e']

# print list
print("Our List: ", MyList)

# Check if 'a' exists in the list or not
if 'a' not in MyList :
 print(" item 'a' is not present in the list")
else:
 print(" 'a' is present in the list")

Output:

Our List: ['a','b','c','d','e']
'a' is present in the list

Execution Time: 0.0009 (Seconds)

Explanation:
In the above example, we used the ‘not in’ operator to check whether ‘a’ exists in MyList or not. We used the if-else condition to print the result. The not in operator checks for if ‘a’ was not in the MyList. Since it is present in the list, the else block is executed. If ‘a’ was not present in MyList the if block would have been executed.

3. Using list.count() function

list.count(x)

We use count() function to count ‘x’ item in the list and returns the occurrence count of ‘x’ item in the list. If the occurrence count is greater than 0, it means ‘x’ item exists in the list.

Example:

# Python3 code
# Check if element exists in the list
# Using in Operator

# Initialization of list
MyList = ['a','b','c','d','e']

# print list
print("Our List: ", MyList)
# Check if 'g' exists in the list or not using count()
if MyList.count('g') > 0 :
	print(" 'g' is present in the list")
else:
	print(" 'g' is not present in the list")

Output:

Our List: ['a','b','c','d','e']
'g' is not present in the list

Execution Time: 0.0019 (Seconds)

Explanation:
In the above example we used the count() function. This function returns the no. of time an object occurs in a sequence. In this case ‘g’ does not occur even a single time thus, else block is executed.

4. Using a Custom Function

Finding an item in a sequence without using any in-built function. The code is discussed briefly in the explanation section.

Example:

# Python3 code
# Check if element or number exists in the list
# Using for loop and if statement

# Initialization of list
MyList = ['a','b','c','d','e']

# Initialization a Flag variable
Counter=0

# print list
print("Our List: ", MyList)

# Run for loop
for i in MyList:
 	if(i == 'a') :
  	# If found initialize valuefound to 1
    		Counter=1
   
# Check if "valuefound" variable is set to 1   
if(Counter== 1) : 	
	print(" 'a' is present in the List")
else:
 	print(" 'a' is not present in the List")

Output:

Our List:  ['a', 'b', 'c', 'd', 'e']
'a' is present in the List

Execution Time: 0.0009 (Seconds)

Explanation:

In the above code, we used the for loop for iterating over the sequence i.e ‘MyList’. Then inside the for loop we used a if block which checks for every value of ‘i’ whether the item exists in the list or not. If it exists the if block sets the value of ‘counter’ to 1.

Outside the for loop we again used the if-else block to check for the value of ‘counter’. If counter value is 1 then if block is executed or else, else block is executed.