22.4 C
New York
Friday, April 26, 2024
HomeProgrammingPythonPython For Loop Index With Examples

Python For Loop Index With Examples

In this tutorial, you will learn Python for loop index with examples and how to get the index and values with respect to the index in for loop. Learn more.

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

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

A list is a data structure in Python that stores 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 an index:

Method 1: Using the 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 the range() function

This method uses 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 a 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 a 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)

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 concerning 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 a 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

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 a 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 the enumerate() function

This method uses 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 a 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

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 a 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

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 a 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

Wrapping Up

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

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES