Python: Check If Element Exists In List - STechies
Maybe your like
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?

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 listExecution 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 listExecution 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 listExecution 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 ListExecution 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.
Tag » Add Element If Not In List Python
-
Appending An Id To A List If Not Already Present In A String
-
Python Append To List If Not In List, Which Of The Following Methods ...
-
Append() Vs Extend() Vs Insert() In Python Lists - Stack Abuse
-
Python Append Only Unique To List Code Example
-
Python List Add If Not Present Code Example
-
Python List .append() – How To Add An Item To A List In Python
-
Python's .append(): Add Items To Your Lists In Place
-
How To Check If An Element Is Not In A List In Python - Adam Smith
-
5. Data Structures — Python 3.10.5 Documentation
-
Python Add Element To List (10 Examples) - Tutorials Tonight
-
Check If Element Exists In List In Python - GeeksforGeeks
-
Check Element Not In A List In Python | Delft Stack
-
Python - Add List Items - W3Schools
-
Python List Contains: How To Check If Item Exists In List - AppDividend
-
Working With Lists In Python - ZetCode
-
11. Lists — How To Think Like A Computer Scientist
-
9. Lists — How To Think Like A Computer Scientist - Open Book Project
-
How To Add Elements To A List In Python - JournalDev