Skip to main content
Python-Check-element-in-List

Python – Check Element Exist in List

This python tutorial help to solve common python list element problem. I’ll let you know, how to check list contains an item, the length of a list, the item exist or not into item, etc.

The List is a collection that is ordered and changeable. It’s defined in a python application using square brackets. You can store elements of all the datatypes as a collection into python List.

You can also checkout other python list tutorials:

Python list contains an item

We can use if the statement to the checklist is empty or if any items exist or are not on a list.

list_i = ("item1", "item2", "item3")
if list_i:
	print("Yes! the list contains items")

Output :
Yes! the list contains items

Check if an element exists in the list using python “in” Operator

The python has the "in" operator to check any element exist in the list.

list_i = ("item1", "item2", "item3")
if 'item2' in list_i :
	print("Yes, 'item2' found in List : " , list_i)

How To Check if Element is not in List

The python has a “not in” operator to check element is not in the list.

list_i = (1, 2, 3, 4, 2, 6)
if 7 not in list_i: 
	print("Yes, 7 NOT found in List : " , list_i)

Occurrence count of given element in the list

You can count the occurrence of an element into the list using count() method.

list_i = (1, 2, 3, 4, 2, 6)
print("Yes, 2 found in List : " , list_i.count(2))

Leave a Reply

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