Python is an extremely high-level programming language that avoids anything closely approaching internal data structures. As a result, we normally don’t require a list’s indices to access its components, but they can come in handy at times. List elements in Python are arranged depending on index values. As a result, we can use their indexes to access each list item or value. In this article, we will look at various methods for accessing an index in Python’s for loop.
Example 1: Using enumerate
enumerate() is a built-in Python function that may be used to access both the values and the indices of a list. This method appends a counter to an iterable and returns it as an enumerated object. The enumerate() function is the quickest and most efficient way to get the index in a for loop. It is also the most popular method of simultaneously accessing both elements and their indices.
Example
my_list = ['Zero', 'One', 'Two', 'Three', 'Four']
print ('Indices and values in the list:')
for index, val in enumerate(my_list):
   print((index, val))
Output
Indices and values in the list:
(0, 'Zero')
(1, 'One')
(2, 'Two')
(3, 'Three')
(4, 'Four')
Explanation
In the above program, we have passed two loop variables index and val in the for loop. These variables can be given any name you like. And within the for loop block, we print the variables that are necessary.
Example 2: Start the indexing with a non-zero value
Furthermore, if we want the indexing to begin with another number, we can adjust the indexing by using the start parameter. It is currently 0-based. Let’s alter it such that it starts at 2.
Example
my_list = ['Zero', 'One', 'Two', 'Three', 'Four']
print ('Indices and values in the list:')
for index, val in enumerate(my_list, start = 2):
   print((index, val))
Output
Indices and values in the list:
(2, 'Zero')
(3, 'One')
(4, 'Two')
(5, 'Three')
(6, 'Four')
Example 3: Without using enumerate()
The most straightforward and often used approach for accessing the index of elements in a for loop is to iterate through the list’s length, increasing the index. We access the list on that index with each increase. This can be accomplished by combining range() and len() as shown below.
Example
my_list = ['Zero', 'One', 'Two', 'Three', 'Four']
print ('Indices and values in the list:')
for i in range(len(my_list)):
   val = my_list[i]
   print((i, val))
Output
Indices and values in the list:
(0, 'Zero')
(1, 'One')
(2, 'Two')
(3, 'Three')
(4, 'Four')
Example 4: Using zip() function
Another option is to use the zip() method, which provides an iterator of tuples that group elements from each of the sequences together. The zip() function addresses the issue of looping across numerous sequences. It takes two or more parameters, all of which must be iterable.
Example
a = [1, 2, 3, 4]
b = ['Car', 'Bike', 'Bus', 'Plane']
print ('Indices and values in the list:')
for k, v in zip(a, b):
   print((k, v))
Output
Indices and values in the list:
(1, 'Car')
(2, 'Bike')
(3, 'Bus')
(4, 'Plane')
Another method to use the zip() function is by implementing it with the range and len functions. As the first parameter of the zip() method, we passed a sequence of numbers ranging from 0 to len(a), and ‘a’ as the second parameter. Using a for loop, we paired each index with its associated value and printed them as tuples.
Example
a = ['Car', 'Bike', 'Bus', 'Plane', 'Truck']
print ('Indices and values in the list:')
for k, v in zip(range(len(a)), a):
   print((k, v))
Output
Indices and values in the list:
(0, 'Car')
(1, 'Bike')
(2, 'Bus')
(3, 'Plane')
(4, 'Truck')
Example 5: Using map() function
Finally, there is the map() function, which applies a function to each item in the series and returns the results.
Example
a = ['Car', 'Bike', 'Bus', 'Plane', 'Truck']
print ('Indices and values in the list:')
result = map(lambda x: (x, a[x]), range(len(a)))
print(list(result))
Output
Indices and values in the list:
[(0, 'Car'), (1, 'Bike'), (2, 'Bus'), (3, 'Plane'), (4, 'Truck')]
Leave a Reply