Python For Loop with Index

In this post, we’ll go over a different approach on how to access an index in Python’s for-loop statements. And remember, you can use for-loop statements only at iterable-object such as a list, tuple, dictionary, set, and string.

To access the index in Python’s for-loop statement in this post, you can use:

  • manual indexing variable
  • enumerate function
  • range function
  • list comprehensions
  • zip function
  • map lambda function

Let’s go through the examples to demonstrate how to use a for-loop statement and access the index in the loop:

For-loop statement with a manual index variable

The easiest method to access an index of elements in a for-loop statement is to add one incrementing variable to save the index of the current element.

Example #1:

#manual indexing
fruits = ['banana', 'pineapple', 'papaya']
index = 0
for fruit in fruits: 
    print('index: {}, fruit: {}'.format(index, fruit))
    index += 1

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

For-loop statement with range function

The most popular method to access an index of elements in a for-loop statement is to go through the list’s length. With this range() function, we can access the elements of the list in order of 0..len(iterable_object)-1.

Example #2:

#range(start, stop, step)
fruits = ['banana', 'pineapple', 'papaya']
for index in range(len(fruits)): 
    print('index: {}, fruit: {}'.format(index, fruits[index]))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

For-loop statement with enumerate function

Sometimes you do want a variable that changes on each loop iteration rather than creates an incrementing variable yourself. You can use Python’s built-in enumerate() function that allows us to loop over a list and retrieve both the index and the value of each item in the list.

Example #3:

#enumerate(iterable_object, start=0)
fruits = ['banana', 'pineapple', 'papaya']
for index, fruit in enumerate(fruits): 
    print('index: {}, fruit: {}'.format(index, fruit))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

For-loop statement using List Comprehensions

You can use list comprehensions to access the index in the for-loop statement but using the enumerate() function is still the effective approach in my opinion.

List Comprehensions Syntax:

new_iterable_object_with_index = [ (index, iterable_object[index]) for index in range(len(iterable_object))]

Example #4:

#using List Comprehensions
fruits = ['banana', 'pineapple', 'papaya']
fruits_with_index = [(index, fruits[index]) for index in range(len(fruits))]
for index, fruit in fruits_with_index: 
    print('index: {}, fruit: {}'.format(index, fruit))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

For-loop statement with zip function

Another approach is to use zip() Python’s built-in function. Actually, the zip() function is to pair two or more iterable-object with the least length that will be decided as the length of the new returned zip object. And later you can change this zip object to a list, tuple, or dictionary with list(), tuple(), or set() function.

Zip Function Syntax:

zip(iterator1, iterator2, iterator3 ...)

Example #5:

#zip(iterator1, iterator2, iterator3 ...)
fruits = ['banana', 'pineapple', 'papaya']
index = [0, 1, 2]

for index, fruit in zip(index, fruits): 
    print('index: {}, fruit: {}'.format(index, fruit))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

The example below will show you; the least length of iterable-object will be used as the length of the new iterable-object:

Example #6:

#zip(iterator1, iterator2, iterator3 ...)
fruits = ['banana', 'pineapple', 'papaya']
index = [0, 1, 2, 3, 4]

new_fruits = [fruit for fruit in zip(index, fruits)]

for idx, fruit in new_fruits: 
    print('index: {}, fruit: {}'.format(idx, fruit))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

Or for more short code, you can see the code below:

Example #7:

#zip(iterator1, iterator2, iterator3 ...)
fruits = ['banana', 'pineapple', 'papaya']
for index, fruit in zip(range(len(fruits)), fruits):
    print('index: {}, fruit: {}'.format(index, fruit))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

For-loop statement with map function

Lambda expression with map() function will map iterable-object to a given function to each item of a given iterable-object, and then return a map object. And later you can change this map object to a list, tuple, or dictionary with list(), tuple(), or set() function.

Map Function Syntax:

map_object = map(lambda i: (i, iterable_object[i]), range(len(iterable_object)))

Example #8:

#using lambda expression with map

fruits = ['banana', 'pineapple', 'papaya']

for index, fruit in map(lambda i: (i, fruits[i]), range(len(fruits))): 
    print('index: {}, fruit: {}'.format(index, fruit))

The Output:

index: 0, fruit: banana
index: 1, fruit: pineapple
index: 2, fruit: papaya

Conclusion

There are many ways to work with Python’s for-loop index. Besides the basic method (with incrementing variable and range function), you can use three methods that rely on Python’s built-in function: enumerate(), zip(), and map() which joined together the indices and their corresponding values into tuples. Then, you can convert those tuples into lists and printed them on the standard output.

Another method that you can use is list comprehensions, but if your code is too complicated better to look up another approach because complicated list comprehensions can lead to a lot of messy code.

Leave a comment