How To Find The Index Of An Item In A List In Python?

On this page, there is a discussion on how to find the index of an item in a list in python. Lists can contain any type of value and hold those values in order. Each order possesses an index location, and every by default index location starts from the 0 element/item in the list in Python.  This article will shed some light on how you can do it in Python with just a few simple steps. In this article, we’ll go over how to find the index of an item in a list in python.

  • Working on Jupyer Notebook Anaconda Navigator.

If you want to learn more about Python Programming, visit Python Programming tutorials.

Grabbing the index of an item in a List Using [ ].

Passing index location as an argument in a List Built-in function and printing it. Passing index location in a List can be the easiest way to grab a specific item in a list. 

list = ['Eda', 'David', 1, 2]
print ("original lists: ", list)
print ("printing index of item in list: ", list[1])

At index 2, item/element ‘David’ exist. 

Grab the index of an item in a list using the index() function

In Python, it’s easy to find the index of an item in a list – all you have to do is use the built-in function ‘index()’. 

Here on index location “2”, “Entechin” exist. 

url = ['www','.','Entechin','.','com'] 
url_index = url.index('Entechin')
url_index
2

another way to do a similar task!

#definig a nested list
url = [['www.'], ['Entechin'], ['.com']] 
# define a list of elements whom index location is to be found
url_index = ['Entechin','.com'] 
index = [[i[0] for i in url].index(j) for j in url_index]
index
[1, 2]

Using the Enumerate function 

In computing, a list is a collection of values or items. Commas (,) and square brackets ([]) separate the items in the list. Furthermore, Python’s list() function returns a list from any iterable. Iterables are objects you can iterate over in Python.

However, grabbing the index of an item in a list is possible by introducing enumerate()  function in a list. This function indexes/tags elements in lists, strings, and tuples. This results in a number that can be indexed in a specific way. 

Moreover, By using the enumerate function in Python, you create an enumerate object from a data collection object. 

The execution process goes in the following sequence:

  • Creating a List 
  • for loop to tell the pythonic command that we want to enter a for a loop.
  • (i,j) a variable represents the current item we are on in the iteration.
  •  List to return object value in the list. 
  • Indices we want to print on output is comes in square brackets.
  • And print the command to print the output of the iterator we want to repeat over and over. 
  • By default counter in enumerate starts from the 0 index location. 
time = [5,10,15,25,30,35,40,45,50,55]
for i,j in list(enumerate(time))[0:10]:
    print ("minute hand at %i:  %i min" %(i,j))
minute hand at 0:  5 min
minute hand at 1:  10 min
minute hand at 2:  15 min
minute hand at 3:  25 min
minute hand at 4:  30 min
minute hand at 5:  35 min
minute hand at 6:  40 min
minute hand at 7:  45 min
minute hand at 8:  50 min
minute hand at 9:  55 min

– The 1st element in the list, “5”, has an index of 0.

– The 2nd element in the list, “10”, has an index of 1.

– The 3rd element in the list, “15”, has an index of 2. and so on…

– The output shows that at index 0…9 in the list, element values from 5…55 respectively present. 

without Enumerate function using if-else conditions

Using an if-else statement, grabbing a specific index location is possible by passing an index location in a square bracket within the condition. 

In addition, You’ll receive an error if Python can’t find the item you’re searching for in the list, a ValueError. Moreover, If there is no item in the list matching your search criteria, then it doesn’t exist. However, Injecting a command of a try/except block before the call to index() might prevent this.

lst= [3,6,9,12]
if lst:
    print("the element comes at index 0:", lst[3])
else:
       print("there is no element")
try:
    print(lst.index([4]))
except ValueError:
    print("That item does not exist")
the element comes at index 0: 12
That item does not exist

pop() to grab the index of an item in a list in Python. 

With the index of the element, call the pop() method to remove an item from a list. Pop() returns and removes the last element from the list if there is no index value passed. 

In this example, you will learn how to use pop() to retrieve and remove items “start, and stop”.

The execution process goes in the following manner:

  • Using while and break iteration the task to grab the index of an item in a list is performed. 
  • The value passed in the pop() function is 0, which means that iteration starts from the index location 0.
  • And if the condition, specifies that during execution if there is ‘stop’ occurs, terminate the process there, that’s why the ‘break’ command introduces in the process. 
  • However, the output reads from the index location 0 and reads till the break calls at ‘stop’. 
url=['www.', 'Entechin', '.com', 'stop']
while url:
    site=url.pop(0)
    if site=='stop':
        break
    print(site)
www.
Entechin
.com

Conclusion

In this article, there are different ways are executed with source code and examples about how you can grab the index location in a list in Python, however, these methods include the “enumerate(), without enumerate() function, pop() method, if-else condition, using index() function and using brackets [] approach”. Moreover, here we learned that How pythonically you can do it in the Programming language.

Leave a Comment

Your email address will not be published. Required fields are marked *