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:
To dive deeper into this topic, I’d love to see you watch my explainer video on the membership operators here: 👇
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
The following example searches the string element
'Sergey' in the list
my_list and returns the index of the first occurrence, i.e.,
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
list()to return a List of Duplicates found.
- Method 3: Use a
forloop 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] print(dups)
Here’s the duplicates from list:
['ollie3', 'shoeguy', 'kyliek', 'ollie3', 'shoeguy', 'kyliek']
🌍 Recommended Tutorial: Python List Find Duplicates
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.