Python

How to Get the Position of Element in a List in Python

Python is a vast language; it has several available built-in functions for lists. One of the important functions we want to perform on a list in Python is to locate the index of a particular element in the list for various purposes. In Python we locate the index of a specified element through the index() method. In this article we will discuss how to get the position of a specific element through index() method with different scenarios in detail.

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:

$ nano python_file.py

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.index(“element”, start_position, end_position)

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:

list.index(“element”)

To get the index of ubuntu in list of linux_OS, write the below mentioned code in “python_file.py”:

print("Find the index of /"ubuntu/" in list /”linux_OS/"")

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:

$ python3 python_file.py

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”:

print("Find the index of "Ubuntu" in list \”linux_OS"")

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:

$ python3 python_file.py

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”:

print("Find the index of /"ubuntu/" in list /”linux_os/" between index2 and index5")

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:

$ python3 python_file.py

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:

print("Find the index of /"Suse /" in list /”linux_os/"")

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:

$ python3 python_file.py

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:

print("Find the index of "Suse " in list \”linux_OS"")

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:

$ python3 python_file.py

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.

About the author

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.