Python Find in List [Ultimate Guide]

5/5 - (4 votes)

When Google was founded in 1998, Wallstreet investors laughed at their bold vision of finding data efficiently in the web. Very few people actually believed that finding things can be at the heart of a sustainable business — let alone be a long-term challenge worth pursuing.

We have learned that searching — and finding — things is crucial wherever data volumes exceed processing capabilities. Every computer scientist knows about the importance of search.

And even non-coders don’t laugh about Google’s mission anymore!

⭐⭐⭐ This article will be the web’s most comprehensive guide on FINDING stuff in a Python list. ⭐⭐⭐

It’s a living document where I’ll update new topics as I go along — so stay tuned while this article grows to be the biggest resource on this topic on the whole web!

Let’s get started with the very basics of finding stuff in a Python list:

Finding an Element in a List Using the Membership Operator

You can use the membership keyword operator in to check if an element is present in a given list. For example, x in mylist returns True if element x is present in my_list using the equality == operator to compare all list elements against the element x to be found.

Here’s a minimal example:

my_list = ['Alice', 'Bob', 'Sergey', 'Larry', 'Eric', 'Sundar']

if 'Eric' in my_list:
    print('Eric is in the list')

The output is:

Eric is in the list

Here’s a graphical depiction of how the membership operator works on a list of numbers:

Figure 1: Check the membership of item 42 in the list of integers.

To dive deeper into this topic, I’d love to see you watch my explainer video on the membership operators here: πŸ‘‡

Python "in" & "not in" Membership Operators [Ultimate Guide]

But what if you don’t want to get a Boolean result on whether the element exists in the list but a specific index of that element? Read on! πŸ‘‡

Python List Find Element

To find an element in a list, Python has the built-in list method index(). You can use it to search for an element and return the index of the element. If the element doesn’t exist in the list, the method will return a ValueError.




The following example searches the string element 'Sergey' in the list my_list and returns the index of the first occurrence, i.e., 2.

my_list = ['Alice', 'Bob', 'Sergey', 'Larry', 'Eric', 'Sundar']

# Element to be searched
element = 'Sergey'

# Search element in the list
index = my_list.index(element)

# Printing the index of the element
print('Element found at index:', index)

This leads to the following output:

Element found at index: 2

To learn more about this approach of finding elements in a Python list, read on here: πŸ‘‡

🌍 Recommended Tutorial: Python List Find Element

Python List Find Duplicates

πŸ’¬ Question: How to check if a list contains duplicate elements?

There are five general ways to handle duplicates in lists:

  • Method 1: Use set() and List to return a Duplicate-Free List
  • Method 2: Use set(), for loop and list() to return a List of Duplicates found.
  • Method 3: Use a for loop to return Duplicates and Counts
  • Method 4: Use any() to check for Duplicates and return a Boolean
  • Method 5: Use List Comprehension to return a List of all Duplicates

I’ll quickly give you the best one — but feel free to check out our full guide to learn more details about these approaches:

users = ['AmyP', 'ollie3', 'shoeguy', 'kyliek', 'ollie3',
         'stewieboy', 'csealker', 'shoeguy', 'cdriver', 'kyliek']

dups = [x for x in users if users.count(x) >= 2]

Here’s the duplicates from list:

['ollie3', 'shoeguy', 'kyliek', 'ollie3', 'shoeguy', 'kyliek']

🌍 Recommended Tutorial: Python List Find Duplicates