Register Login

Fetch the Index of the Items or List Using for Loops in Python

Updated May 15, 2023

In programming, retrieving the index from the elements in a list is one of the typical practices allowing users to access specific items for particular applications.

Python provides different methods to fetch the index of the items using for loops. This article will discuss all these methods in Python and how these work with for loop and the respective code examples with explanations.

What does for loop do?

A for loop helps us to loop and iterate over the data items of a sequence, such as a list, string, or tuple in Python. Here, we will use for loop to access the elements using their index.

The for loop will run a code block for each element of this Python list. Users can use the loop variable, also called the index, to refer to the current elements in the list.

Following are the four methods in Python with for loop to access the elements of the list using the index:

Method 1: Using enumerate() method to access their index

The enumerate() method is a built-in method in Python that allows users to record the number of iterations in the for a loop.

Code Snippet:

l = [80, 75, 92, 69, 77, 82]
print("The indices and elements in l:")
for index in enumerate(l):
	print(index)

Output:

Explanation:

The enumerate() method counts the number of iterations from the start (by default zero) and returns a value it fetches from the iteration.

Method 2: for loop with the range() method

Using the range() method, we can retrieve the sequence of numbers that starts from zero by default. By default, it will continue returning the sequence incrementing by one and precludes before the specified number.

Code Snippet:

l = [80, 75, 92, 69, 77, 82]
print("The indices and elements in l:")
for i in range(len(l)):
    print(i, l[i])

Output:

Explanation:

Here, the range() method iterates over each element and returns the list of an index with the respective elements. Users can use the index element to define the location of the elements in the list. Here, we will access the index of each element through looping.

We will use an iterator variable to loop over the Python list in this example.

Method 3: Using the zip() method

The zip() method accepts iterable elements as the arguments and returns the iterator. We can specify the zip() method as zip(*iterables).

Code Snippet:

l = [80, 75, 92, 69, 77, 82]
for index in zip(range(len(l)), l):
    print(index)

Output:

Explanation:

In the above code, we are using the range() method to generate a list of indexes, and the zip() method combines them from the range method and the elements from the list "l." The zip() method accesses the index of the elements in Python.

Method 4: Using the list comprehension method to access an index

Users can also use the list comprehension method to create and define the list of indexes from the existing elements in a Python list.

Code Snippet:

l = [80, 75, 92, 69, 77, 82]
print(" Here, we retrieve the index of each elements: ")
print([index for index in range(len(l))])
print(" Here, we display the elements in list: ")
print([l[index] for index in range(len(l))])

Output:

Explanation:

Using this method, we can fetch both the elements and their respective indexes.

Conclusion:

Finally, we hope this article has wrapped up all the methods in Python that helps users to access index of the elements of a list using for loop. This article covers the following method:

  • The range() method
  • The enumerate() method
  • The zip() method
  • The list comprehension method


×