Python for loop with index


In this tutorial, you will learn how to use Python for loop with index. We frequently need the index value while iterating over an iterator but Python for loop does not give us direct access to the index value when looping over an iterator.

    Table of Contents

  1. Quick Learning
  2. The enumerate() function
  3. Python for loop with index using enumerate() function
    1. Getting custom index value using enumerate
  4. Get Index using the range function
  5. Get index using the zip function
  6. Conclusion

Quick Learning

To get the index value of an iterator in a for loop use enumerate() function.

Let's see how to use enumerate() function in a for loop.

list = ["apple", "banana", "cherry", "orange", "kiwi"]

# getting index value
for index, item in enumerate(list):
    print(index, item)

Output

0 apple
1 banana
2 cherry
3 orange
4 kiwi

There are many ways to get the index value of an iterator in a for loop we will discuss all in this tutorial.

The above code will print the index value and the item value of the list. It is a brief solution for the problem for a better understanding of the code and concept continue reading the tutorial.


The enumerate() function

The enumerate() function is a built-in function in Python. It returns an enumerated object. This object will iterate through the list and create a tuple containing the index value and the item value of the list.

To visualize the concept of enumerate function, let's see how it works.

list = ["apple", "banana", "cherry", "orange", "kiwi"]

# creating an enumerate object
obj = enumerate(list)

# print the object as list
print(list(obj))

Output

[(0, 'apple'), (1, 'banana'), (2, 'cherry'), (3, 'orange'), (4, 'kiwi')]

You can see in the code above that the enumerate function couples the index value and the item value of the list in a tuple.

enumerate function
enumerate function

The enumerate() function takes two parameters, the first one is the list and the second one is the start value of the index value. If the start value is not provided, the default value is 0.

You can use the second parameter to start indexing from a specific value like 1.

list = ["apple", "banana", "cherry"]

# indexing from 1
obj = enumerate(list, 1)

print(list(obj))

Output

[(1, 'apple'), (2, 'banana'), (3, 'cherry')]

Python for loop with index using enumerate() function

You have seen above how to use enumerate() function in a for loop. Now let's see how to get the index value in a for loop.

list = ["apple", "banana", "cherry", "orange", "kiwi"]

# python for loop with index
# using enumerate function
for index, item in enumerate(list):
    print(f'{index}: {item}')

Output

0: apple
1: banana
2: cherry
3: orange
4: kiwi

In the loop above we have access to the index value of the list. You can use the index value to do something useful. We have just printed it here.


Getting custom index value using enumerate

Python works on a 0 index system so the default index value generated by the enumerate function is 0. But you can get a custom index value by passing the second parameter to the enumerate function.

list = ["apple", "banana", "cherry", "orange", "kiwi"]

# python for loop index start at 1
for index, item in enumerate(list, 1):
    print(f'{index}: {item}')

Output

1: apple
2: banana
3: cherry
4: orange
5: kiwi

You can see the index value started from 1 rather than 0 in the above code.


Get Index using the range function

The range() function in python is used to generate a list of numbers.

The function takes a number as a parameter and it will generate a list of numbers from 0 to the number passed to the function. For example, range(5) will generate a list of numbers from 0 to 4.

You can use this to pass the length of the list and in return, you will get a list of numbers which you can use in for loop and access both the index and the item value.

Here is an example that uses the range function in for loop to get the index value.

list = ["apple", "banana", "cherry", "orange", "kiwi"]

# python for loop with index
for index in range(len(list)):
    print(f'{index}: {list[index]}')

Output

0: apple
1: banana
2: cherry
3: orange
4: kiwi

When you have access to the index value then you can get its corresponding item value by list[index].


Get index using the zip function

The zip() function in python merges two lists into one list of tuples.

The function takes two iterable as parameters and it will merge them into one list of tuples. For example, zip(list1, list2) will merge the two lists into one list of tuples.

You can use this to pass the length of the list and in return, you will get a list of tuples which you can use in for loop and access both the index and the item value.

Here is an example that uses the zip function in for loop to get the index value.

list1 = ["apple", "banana", "cherry"]
index = [0, 1, 2, 3, 4]

# using zip to get index
for index, item in zip(index, list1):
    print(f'{index}: {item}')

Output

0: apple
      1: banana
      2: cherry

Conclusion

In this tutorial, we have learned how to use enumerate() function in python and use it to get the index value in python for loop.

You can use range() and zip() function to get index value in python for loop.

Reference: enumerate() function