The index() method can be used in python on any operating system but in this article we will discuss its implementation on Ubuntu (Linux system).
Requirement
Any version of Python installed on your Ubuntu (python3 is pre-installed on the latest version of Ubuntu).
Creating a Python file
To code in Python on Ubuntu, you have to create a Python file with “.py” extension by below mentioned command:
You can change the name of the file.
Methods to find index of specific element in list in Python are explained below:
How to Find Position of Specific Element In Python List:
Find the index of specified element which is present in list by below mentioned syntax:
list: Name of list in which you will find index of specified element
element: The specific element which is to be searched in the list
start_position(optional): Begin searching the element from this index in list
end_position(optional): Stop searching for this position in the list
Example1:How to Find Index of Specific Element which is present in Python List
Below mentioned is syntax to find specified element without optional arguments od index() method:
To get the index of ubuntu in list of linux_OS, write the below mentioned code in “python_file.py”:
linux_OS=['debian','ubuntu','centOS','fedora']
index=linux_OS.index('ubuntu')
print("The index of specified element is: ",index)
To get the desired output, run the “python_file.py” by below mentioned command:
Example2: How to Find Index of Element with more than one occurrence in Python List
If the specified element exist more than one time in list then index() method will give the index of its first occurrence, to find the index of “ubuntu” in list of “linux_OS”, write the below mentioned code in “python_file.py”:
linux_OS=['debian','ubuntu','centOS','fedora','ubuntu']
try:
index=linux_OS.index('ubuntu')
print("The index of specified is: ",index)
except ValueError:
print("The specified element not found!!")
To get the desired output, run the “python_file.py” by below mentioned command:
The above output shows that the index() method gives the first occurrence index of the specified element.
Example3: How to Find Index of Element in List with optional arguments in Python List
We can find the index of the specified element by mentioning start and end index in the list as shown in the below example.
To find the index of “ubuntu” in list of “linux_OS” between “index2” and “index5”, write the below mentioned code in “python_file.py”:
linux_OS= ['debian','ubuntu','centOS','fedora','ubuntu']
try:
index=linux_OS.index('ubuntu',2,5)
print("The index of specified element is: ",index)
except ValueError:
print("The specified element not found")
To get the desired output, run the “python_file.py” by below mentioned command:
The above output gives the index of the second ‘ubuntu’ occurrence in the list as it is available between specified indexes.
What to do if Element is not Present in Python List
If specified element is not present in list then a ValueError exception of “value not found” is raised and we need to handle that exception using “try except” block
Write below mentioned code in “python_file.py” to find index of ‘Suse’ in “linux_OS” list without try except block:
linux_OS= ['debian', 'ubuntu', 'centOS' ,'fedora']
index=linux_OS.index('Suse')
print("The index of specified element is: ",index)
To get the desired output, run the “python_file.py” by below mentioned command:
The above output gives the “ValueError” exception as the element is not present in the list but you don’t have to worry, handle the exception through the “try except” block.
Now again write the above code with “try except” block to handle exception as mentioned below:
linux_OS=['debian','ubuntu','centOS','fedora']
try:
index=linux_OS.index('Suse')
print("The index of specified element is: ",index )
except ValueError:
print("The specified element not found!!" )
To get the desired output, run the “python_file.py” by below mentioned command:
You need to insert this “try except” block while using index() method.
Conclusion
In Python, the index() method is used to find the index of a specified element. In this article, index() method is discussed, which is used to find the index of specified element with start and end index(optional) and if element is not present then it throws an exception which needs to be handled using the “try except ” block. This article will help you to find the index of a list easily and use the index for several purposes in Python.