Python For Loop Index With Examples

82

This tutorial will discuss Python for loop index and how to get the index and values with respect to the index in for loop.

We will consider a list of elements and then iterate through the list using for loop and get the index and value with respect to the index.

A list is a data structure in Python used to store elements with multiple data types. It is represented by [].

For loop is used to iterate the elements. Indexing starts with 0. and ends with the length of the list. So to get the last element, we can say the length of the actual list-1.

List Syntax:

my_list=[element1,element2,....,element n]

where elements represent values.

Also ReadHow to Reverse an Array In Python [Flip Array]

Python For Loop Index

We will cover the following methods to iterate through a for loop in Python using index:

Method 1: Using range() function

Method 2: Zip() function with range() function

Method 3: Using for loop index and value

Method 4: Using for loop index starting with 1

Method 5: Using enumerate() Python function

Method 6: Using for loop index starting with 0

Method 7: Using for loop index backward

Now, let’s see these different methods to use Python for loop index.

Method 1: Using range() function

This method use range() to iterate over each element in a list.

Syntax:

for index_iterator in range(len(my_list)):
   print(index_iterator)
   print(my_list[index_iterator])

where,

  1. my_list is an input list
  2. index_iterator is used to iterate indices
  3. my_list[index_iterator] is used to get the elements present at a particular index.

Also ReadHow to Check If Dict Has Key in Python [5 Methods]

Example:

This example will create a list with five integer elements and get the index and values using for loop.

#create a list of 5 integer elements
my_list = [34,45,43,89,76]
 
#for loop index
for i in range(len(my_list)):
  #display the index and element with respect to index
   print(i,"-->", my_list[i])

Output:

0 --> 34
1 --> 45
2 --> 43
3 --> 89
4 --> 76

Also ReadHow to Convert Binary to Decimal in Python [5 Methods]

Method 2 : Using zip() with range() function

This method uses range () and zip() to iterate over each element in a list. Zip () is used to combine the index along with its value.

Syntax:

for index_iterator in zip(range(len(my_list)), my_list):
        print(index_iterator )

where,

  1. my_list is an input list
  2. index_iterator is used to iterate indices

Example: This example will create a list with five integer elements and get the index and values using for loop with zip().

#create a list of 5 integer elements
my_list = [34,45,43,89,76]
 
#for loop index with zip along with range()
for i in zip(range(len(my_list)), my_list):
        print(i)

Output:

(0, 34)
(1, 45)
(2, 43)
(3, 89)
(4, 76)

Also ReadPython Program To Display Fibonacci Series Up To N Terms

Method 3: Using for loop index and value

This method uses the for loop and an index iterator to iterate the indices and display the values with respect to the index. Finally, we will increment the index iterator to 1 until all the list elements are iterated.

Syntax:

index_iterator=0
for value in my_list:
   print(index_iterator,"-->",value)
   index_iterator += 1

where,

  1. my_list is an input list
  2. index_iterator is used to iterate indices

Example:

This example will create a list with five integer elements and get the index and values using for loop with the index value.

#create a list of 5 integer elements
my_list = [34,45,43,89,76]

#create a variable to iterate
i = 0

#index through for loop
for value in my_list:
  #display index and value
   print(i,"-->",value)
   #increment index value
   i += 1

Output:

0 --> 34
1 --> 45
2 --> 43
3 --> 89
4 --> 76

Also ReadPython Program To Display Multiplication Table of Any Number

Method 4: Using for loop index starting with 1

This method uses the for loop and an index iterator that starts with 1 and iterates the elements from index -1 to the last index of the list.

Syntax:

for index_iterator in my_list[1:] :
  print(index_iterator )

where,

  1. my_list is an input list
  2. index_iterator is used to iterate indices

Example:

This example will create a list with 5 integer elements and get the index using for loop with the index value.

#create a list of 5 integer elements
my_list = [34,45,43,89,76]

#iterate from index 1 through list
for i in my_list[1:] :
  #display index
  print(i)

Output:

45
43
89
76

Also ReadHow To Automate Google Search With Python

Method 5: Using enumerate() function

This method use enumerate() to iterate over each element in a list.

Syntax:

for index_iterator, value in enumerate(my_list, start=1): 
    print(index_iterator,"-->",value)

Where,

1. my_list is an input list

2. index_iterator is used to iterate indices

3. value is used to get the elements present at a particular index

Example:

This example will create a list with 5 integer elements and get the index and values using for loop.

#create a list of 5 integer elements
my_list = [34,45,43,89,76]

#get index and value with enumerate method through for loop
for index, value in enumerate(my_list, start=1): 
    print(index,"-->",value)

Output:

1 --> 34
2 --> 45
3 --> 43
4 --> 89
5 --> 76

Also ReadPython Program To Check If a Number is Odd or Even

Method 6: Using for loop index starting with 0

This method uses the for loop and an index iterator that starts with 0 and iterates the elements from index -1 to the last index of the list.

Syntax:

for index_iterator in my_list[0:] :
  print(index_iterator )

Where,

1. my_list is an input list

2. index_iterator is used to iterate indices

Example:

This example will create a list with 5 integer elements and get the index using for loop with the index value.

#create a list of 5 integer elements 
my_list = [34,45,43,89,76] 

#iterate from index 0 through list 
for i in my_list[0:] : 
  
  #display index 
  print(i)

Output:

34
45
43
89
76

Also ReadPython Program To Check Prime Number

Method 7: Using for loop index backward

We will access the index and values from backward using the reversed() function with the range() function.

Syntax:

for index_iterator in reversed(range(len(my_list))):
   print(index_iterator)
   print(my_list[index_iterator])

Example:

This example will create a list with 5 integer elements and get the index and values using for loop from backward.

#create a list of 5 integer elements 
my_list = [34,45,43,89,76] 

#iterate from backwards
for i in reversed(range(len(my_list))):
  #display the index and value
        print(i,"-->",my_list[i])

Output:

4 --> 76
3 --> 89
2 --> 43
1 --> 45
0 --> 34

Also Read70 Best Online Courses With a Certificate of Completion

Wrapping Up

By this, we came to the end of our tutorial. We learned different ways to loop through the index and elements in a list using for loop. We also saw how to traverse backward with for loop index.

Next Read: