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
Also, you may want to try out the following tutorial on the Finxter blog addressing a very similar topic:
🌍 Recommended Tutorial: How to Find the Index of a List Element in Python?
Find Element That Is Not in the List
If you search for an element that doesn’t exist in the list, such as
'Elon Musk' in the list
my_list, the list
index() method will return a
ValueError to indicate that the element couldn’t be found.
Here’s an example:
my_list = ['Alice', 'Bob', 'Sergey', 'Larry', 'Eric', 'Sundar'] # Element to be searched element = 'Elon Musk' # Search element in the list index = my_list.index(element) # Printing the index of the element print('Element found at index:', index)
Here’s the resulting ValueError Message (in bold):
Traceback (most recent call last): File "C:\Users\...\code.py", line 7, in <module> index = my_list.index(element) ValueError: 'Elon Musk' is not in list
How to Fix “ValueError: Element is not in List”?
To fix the
ValueError: element is not in list, call the function enclosed in a
except block, use a simple conditional statement, or use the
The following example shows these three ways exemplified (see highlighted lines):
my_list = ['Alice', 'Bob', 'Sergey', 'Larry', 'Eric', 'Sundar'] # Element to be searched element = 'Elon Musk' # Try Except Block try: my_list.find('Elon Mustk') except: print('not found') # 2. Conditional Statement index = my_list.find('Elon Musk') if 'Elon Musk' in my_list else -1 print(index) # 3. List Count if my_list.count('Elon Musk') == 0: print('Element not in list')
The output is:
not found -1 Element not in list
I particularly like the second option because it allows you to combine the membership operator
in with the
list.find() method to also allow the search for non-existent elements without raising an error.
Video and List Index Tutorial
You can check out my in-depth guide on the
index() list method that shows many peculiarities, such as its runtime complexity and interesting variations here:
Also, check out our blog tutorial! 👇
🌍 Recommended Tutorial: Python List Index Method
Complete Guide on List Search
Search is one of the most important topics in tech. Every coder must know it at heart. Ask Google!
Python has many advanced variations and tricks to do search in lists and taking some time out of your day to learn those will pay back a hundredfold throughout your career! 🚀
If you want to learn infinite ways to search and find elements in a list, check out our full guide on Finding Stuff in Python Lists 🔍 — it’s the most comprehensive guide on this topic in existence!
🌍 Recommended Tutorial: Python Find in List [Ultimate Guide]
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.