A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/check-if-element-exists-in-list-in-python/ below:

Check if element exists in list in Python

Check if element exists in list in Python

Last Updated : 11 Jul, 2025

In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword.

Example:

Python
a = [10, 20, 30, 40, 50]

# Check if 30 exists in the list
if 30 in a:
    print("Element exists in the list")
else:
    print("Element does not exist")

Output
Element exists in the list

Let's explore other different methods to check if element exists in list:

Using a loop

Using for loop we can iterate over each element in the list and check if an element exists. Using this method, we have an extra control during checking elements.

Python
a = [10, 20, 30, 40, 50]

# Check if 30 exists in the list using a loop
key = 30
flag = False

for val in a:
    if val == key:
        flag = True
        break

if flag:
    print("Element exists in the list")
else:
    print("Element does not exist")

Output
Element exists in the list

Note: This method is less efficient than using 'in'.

Using any()

The any() function is used to check if any element in an iterable evaluates to True. It returns True if at least one element in the iterable is truthy (i.e., evaluates to True), otherwise it returns False

Python
a = [10, 20, 30, 40, 50]

# Check if 30 exists using any() function
flag = any(x == 30 for x in a)

if flag:
    print("Element exists in the list")
else:
    print("Element does not exist")

Output
Element exists in the list
Using count()

The count() function can also be used to check if an element exists by counting the occurrences of the element in the list. It is useful if we need to know the number of times an element appears.

Python
a = [10, 20, 30, 40, 50]

# Check if 30 exists in the list using count()
if a.count(30) > 0:
    print("Element exists in the list")
else:
    print("Element does not exist")

Output
Element exists in the list
Which Method to Choose

8 Methods to Check If an Element Exists in a List | Python Tutorial


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4