Python List Find Element : Chris
by: Chris
blow post content copied from Finxter
click here to view original post
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
.
Syntax:
list.index(element)
Example:
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
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 try
/except
block, use a simple conditional statement, or use the list.count(element)
method.
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]
December 09, 2022 at 06:47PM
Click here for more details...
=============================
The original post is available in Finxter by Chris
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment