Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

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

|| 0

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

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?

Using “in” Operator

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

Example:

# Python3 code
# Check if element exist in list
# Using in Operator

# Initialization of list
liststrings = ['Hy' , 'how', 'at', 'this', 'where', 'from']

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

# Check if element or word "at" in list using "if" and "in" Operator 
if 'at' in liststrings :
print("Yes, element 'at' found in List")

Output:

Our List:  ['Hy', 'how', 'at', 'this', 'where', 'from']
Yes, element 'at' found in List

Using “not in” Operator

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

Example:

# Python3 code
# Check if element exist in list
# Using “not in” Operator

# Initialization of list
liststrings = ['Hy' , 'how', 'at', 'this', 'where', 'from']

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

# Check if element or word "time" in list using "if" and "not in" Operator 
if 'time' not in liststrings :
print("Yes, element 'time' not found in List")

Output:

Our List:  ['Hy', 'how', 'at', 'this', 'where', 'from']
Yes, element 'time' not found in List

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 or character exists in list
# Using count() function

# Initialization of list
liststrings = [a,b,c,d,e,f,g,h]

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

if liststrings.count(e) > 0 :
print("Yes, character e found in List")

Output:

Our List:  [a,b,c,d,e,f,g,h]
Yes, character e found in List

Using for loop and if statement

First we use “for loop” with all the elements of the list and then we will check the element of the value with the help of “if” statement.

Example:

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

# Initialization of list
liststrings = ['Red','Green','Blue','Orange','White']

# Initialization a Flag variable 
valuefound=0

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

# Run for loop
for i in liststrings: 
    if(i == 'Green') : 
        print ("Yes Value or Word 'Green' Exists in List")
        valuefound=1
        
# Check if "valuefound" variable is set to 1       
if(valuefound == 1) : 
print("No Value or Word 'Green' Does not Exists in List")

Output:

Our List:  ['Red', 'Green', 'Blue', 'Orange', 'White']
Yes Value or Word 'Green' Exists in List

 


Advertisement
Advertisement
Advertisement