Python for loop with index [4 Ways & Examples]

In this Python tutorial, we will discuss Python for loop index. Here, we will also cover the examples below:

A for loop in Python is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. The loop variable, also known as the index, is used to reference the current item in the sequence.

Methods for accessing Python for loop index

There are 4 different ways to check the index in a for loop in Python:

  1. Using the enumerate() function
  2. Using the range() function
  3. Using the zip() function
  4. Using the map() function

Method-1: Using the enumerate() function

The “enumerate” function is one of the most convenient and readable ways to check the index in a for loop when iterating over a sequence in Python.

# This line creates a new list named "new_lis" with the values [2, 8, 1, 4, 6]
new_lis = [2, 8, 1, 4, 6]

# This line starts a for loop using the enumerate function to iterate over the "new_lis" list. 
# The loop variable "x" is used to reference the current index, and "new_val" is used to reference the current value.
for x, new_val in enumerate(new_lis):
	# This line prints the current index and value of the item in the list, separated by a comma.
    print (x, ",",new_val)

The above example uses the Python enumerate function to iterate over the new_lis list.

  • Where the Python variable x will be the current element’s index and new_val will be the value of that element.
  • The print statement in Python will print the index and the value of that element separated by a comma, for each element in the list.
Python for loop index
Python for loop index

So, in this section, we understood how to use the enumerate() for accessing the Python For Loop Index.

READ:  Lambda in List Comprehension in Python [3 Examples]

Read: For loop vs while loop in Python

Method-2: Using the range() function

The “range” function in Python can be used to generate a list of indices that correspond to the items in a sequence. This allows us to reference the current index using the loop variable in Python.

# This line creates a new list named "my_lis" with the values [6, 1, 9, 2]
my_lis = [6, 1, 9, 2]

# This line starts a for loop using the range function to iterate over the indices of the "my_lis" list. 
# The loop variable "new_indic" is used to reference the current index.
for new_indic in range(len(my_lis)):
    # This line prints the current index and the value of the element at that index in the list
    print(new_indic, my_lis[new_indic])

In the above example, the range function in Python is used to generate a list of indices that correspond to the items in the “my_lis” list.

  • The for loop iterates over that range of indices in Python, and for each iteration, the current index is stored in the variable new_indic.
  • The element’s value at that index is printed by accessing it from the “my_lis” Python list using the new_indic variable.
Python for loop index range function
Python for loop index range function

So, in this section, we understood how to use the range() for accessing the Python For Loop Index.

Read: Python While Loop Example

Method-3: Using the zip() function

The Python “zip” function can be used to iterate over multiple sequences in parallel, allowing you to reference the corresponding items at each index.

# This line creates a new list named "new_str" with the values ['e', 'f', 'i']
new_str = ['e', 'f', 'i']

# This line starts a for loop using the zip function to iterate over the "new_str" list and a range of indices. 
# The loop variable "m" is used to reference the current tuple of index and value.
for m in zip(range(len(new_str)), new_str):
    # This line prints the current tuple of index and value of the item in the list
    print(m)

In the above example, the range function in Python is used to generate a list of indices that correspond to the items in the “new_str” list.

  • The Python zip function is used to combine the indices from the range function and the items from the “new_str” list and iterate over them in parallel.
  • For each iteration in Python, the current tuple of index and value is stored in the variable m and printed.
Python for loop index zip function
Python for loop index zip function

So, in this section, we understood how to use the zip() for accessing the Python For Loop Index.

READ:  Python Code to Print a Binary Tree

Read: Python While loop condition

Method-4: Using the map() function

The Python“map” function takes a function and an iterable as arguments and applies the function to each item in the iterable, returning an iterator.

The Lambda function in Python passed to “map” can take an additional parameter to represent the index of the current item.

# This line creates a new list named "new_str2" with the values ['Germany', 'England', 'France']
new_str2 = ['Germany', 'England', 'France']

# This line uses the map function to apply a lambda function to a range of indices and items of the "new_str2" list.
# The lambda function returns a tuple of the form (index, value) for each item in the "new_str2" list.
new_out = map(lambda o: (o, new_str2[o]), range(len(new_str2)))

# This line converts the map object to a list and prints it
print(list(new_out))

In the above example, the code creates a Python list named “new_str2” with the values [‘Germany’, ‘England’, ‘France’].

  • The map() function in Python is used to apply a lambda function to a range of indices and items of the “new_str2” list.
  • The Python lambda function takes the index of the current item as an argument and returns a tuple of the form (index, value) for each item in the “new_str2” list.
  • The Python map() function returns an iterator that contains the tuples of index and value for each item in the “new_str2” list. The result of the map function is then converted to a list and printed in Python.
Python for loop index map function
Python for loop index map function

So, in this section, we understood how to use the map() for accessing the Python For Loop Index.

You may also like to read the following Python tutorials.

READ:  String Comparison in Python (Complete Guide)

In this Python tutorial, we will discuss Python for loop index to know how to access the index using the different methods. Here is the set of methods that we covered:

  • Using the enumerate() function
  • Using the range() function
  • Using the zip() function
  • Using the map() function