Python Find in List: How to Search an Element in List

Here are eight ways to check if an element is in the list in Python:

  1. Using in operator
  2. Using enumerate() and for loop
  3. Using list comprehension to get indices
  4. Using index()
  5. Using count()
  6. Using any()
  7. Using filter()
  8. Using for loop
Python Find in List - How to Search an Element in List
Figure 1: Overview of searching an element in Python List

Method 1: Using in operator

Method 1 - Using in operator
Figure 2: Using the “in” operator

Here is an example of how to use the “in” operator.

main_list = [11, 21, 19, 46]

if 19 in main_list:
  print("Element is in the list")
else:
  print("Element is not in the list")

Output

Element is in the list

Method 2: Using enumerate() with for loop

The enumerate() function is often used alongside a for loop to iterate over a sequence while keeping track of the index of the current item. This is particularly helpful when you need to find the index of an element in a list.

Method 2 - Using enumerate() with for loop
Figure 3: Using enumerate() with for loop

Here’s how you can use enumerate() within a for loop to find the index of an element in a list:

main_list = [11, 21, 19, 46]
search_element = 19
found_element = False

for index, item in enumerate(main_list):
  if item == search_element:
    found_element = True
    print(f"{search_element} found_element at index {index}")
    break

if not found_element:
  print(f"{search_element} not found_element in the list")

Output

19 found_element at index 2

Method 3: Using list comprehension

If you are looking for all indices of a certain element in a list, list comprehension is quite handy.

Method 3 Python list comprehension
Figure 4: Use list comprehension to find an index of an element

Here’s an example of how you might use it:

main_list = [11, 21, 19, 46]
search_element = 19

indices = [index for index, item in enumerate(
            main_list) if item == search_element]

if indices:
  print(f"{search_element} found at indices {indices}")
else:
  print(f"{search_element} not found in the list")

Output

19 found at indices [2]

Method 4: Using index()

The list.index() method searches for an element in the list and returns its index.

If the same element is present more than once, the method returns the index of the first occurrence of the element. The index in Python starts from 0, not 1. So, through an index, we can find the position of an element in the list.

Method 4 - Using the index() method
Figure 5: Using the index() method

Here is a small and simple code example of using the list.index() method.

main_list = [11, 21, 19, 46] 

search_element = 19

index = main_list.index(search_element)

print(index)

Output

2

Method 5: Using count()

The list.count() method returns the number of times the given element in the list.

Syntax

list.count(element)

It takes a single argument element, the item that will be counted.

Method 5 - Using the count() function
Figure 6: Using the count() function

Here is a code example to demonstrate the count() method.

main_list = [11, 21, 19, 46]

count = main_list.count(21)

if count > 0:
  print("Element is in the list")
else:
  print("Element is not in the list")

Output

Element is in the list

Method 6: Using list comprehension with any()

The any() function returns True if any element in an iterable is True. Otherwise, it returns False.

Method 6 - Using list comprehension with any()
Figure 7: Using list comprehension with any()

Here is how to use any() function.

main_list = [11, 21, 19, 46]

output = any(item in main_list for item in main_list if item == 22)

print(str(bool(output)))

Output

False

Method 7: Using filter()

The filter() method iterates through the list’s elements, applying the function to each. It returns an iterator that iterates through the elements when the function returns True.

Method 7 - Using the filter() method
Figure 8: Using the filter() method

Here is a code example.

main_list = [11, 21, 19, 46]

filtered = filter(lambda element: element == 19, main_list)

print(list(filtered))

Output

[19]

Method 8: Using for loop

main_list = [11, 21, 19, 46]

for i in main_list:
  if(i == 46):
    print("Element Exists")

Output

Element Exists

Which method to use and why?

I ran a research and experiment to find out which method is fast and efficient for finding an element in the list, and my conclusion is that most of the methods take 0 seconds or no time. That means most of the methods are performant, but when it comes to clear code, “in operator” stands out from the other approaches.

Here is the result of the experiment.

Python Find in List performance experiment

I used the time module to measure the execution time of different approaches or code blocks like this.

For more information about the experiment code, you can check the code I put on Github.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.