Program to find index of element in list in python in Python
In the python programming article, we are going to learn
-
program to find index of element in list in python
Program to find index of element in list in python in Python
The program to find index of element in list in python is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
my_list = [22,11,34,78,61,35,91,77,59]
try:
index_of_91 = my_list.index(91)
print("Element is at {} index position!".format(index_of_91))
except:
print("The element is not present in the list!")
The output of the find index of element in list in python program is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Element is at 6 index position!
Few important tips about the program
1. The index() method of the python list helps us to find the index of an element in python. It takes the element a an argument.
2. But if the element is not present, it raises an exception.
3. To handle that exception we use try-except exception handler here.
find index of element in list in python snapshot:
This wraps up our session on find index of element in list in python.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :
Oct 13,2022