How to access the index inside a for loop in Python?

In this Python tutorial, you will learn to access indices inside a for loop.

Table Of Contents

Introduction

In python, for loop is used to iterate elements of an iterable or sequence. An iterable can be a list, tuple, set, or dictionary. The for loop iterates over a sequence of elements in the iterable.

Syntax of for loop:

for iterator in iterable/sequence:
    statement 1
    statement 2
    .......
    .......
    statement n

Let’s see how to access the index inside for loop using the following methods.

Access the index inside for loop using the index element

In Python, indexing starts with 0. So to get the indices inside for loop, we can iterate over 0 to N, where N is the number of elements in the squence. During iteration we can print the elements and their index positions,

Let’s see the syntax.

for i in range(len(list_of_elements)):
    print(i)

Here,
1. list_of_elements represents the a sequence that contains elements.
2. i is a variable used to iterate over the indices of elements in the list.

If we want to get the element value along with its index, then we can access the list element using index position.

Syntax:

for i in range(len(list_of_elements)):
    print( list_of_elements[i] )

Let’s see the example, to understand it better.

Example:

In this example, we will create a list of string elements. Then we will iterate over this list using a for loop and fetch the index & value of each element during iteration.

# create a list with 5 strings
list_of_elements = ["welcome","to","thispointer","python","tutorial"]

print("Elements Indices: ")

# return the  indices in the list_of_elements
for idx in range(len(list_of_elements)):
    print(idx)

print()

print("Elements present in the list: ")

# return the index values from the list_of_elements
for idx in range(len(list_of_elements)):
    print(list_of_elements[idx])

Output:

Elements Indices: 
0
1
2
3
4

Elements present in the list: 
welcome
to
thispointer
python
tutorial

From the above output, we can see that the index is returned using iterable and using [] we fetched the element value too.

Access the index inside for loop using using enumerate()

In Python, enumerate() function accepts an iterable element as argument and returns an iterable sequence which contains the index position and the value from original given iterable. Each element in this returned iterable is a tuple that contains the index and the value.

Structure:

(index, element)

Let’s see the syntax.

for (idx, value) in enumerate(list_of_elements):
    print(idx)
    print(value)

Here,
1. list_of_elements represents the data that contains elements
2. idx is the index position of element and value contains the value of that element.

Let’s see the example, to understand it better.

Example:
In this example, we are creating a list of string elements in a list and getting the indices and elements using enumerate() function.

# create a list with 5 strings
list_of_elements = ["welcome","to","thispointer","python","tutorial"]


# return the  indices and elements  from the list_of_elements
for (idx, value) in enumerate(list_of_elements):
    print(idx , " :: ", value)

Output:

0  ::  welcome
1  ::  to
2  ::  thispointer
3  ::  python
4  ::  tutorial

From the above output, we can see that the index is returned along with the element in a tuple format.

Access the index inside for loop using List Comprehensions.

In Python, list comprehension is used to iterate over the elements of a sequencein a single line of code. So we can use for loop inside this list comprehension and return the indices. Let’s see the syntax.

[idx for idx in range(len(list_of_elements))]

Here,
1. list_of_elements represents the data that contains elements
2. idx is a variable used to iterate the elements in a list inside for loop.

We can access the elements present at a particular index using [].
Syntax:

[list_of_elements[idx] for idx in range(len(list_of_elements))]

Let’s see the example, to understand it better.
Example:

In this example, we are creating a list of string elements in a list and getting the indices and elements separately using list comprehension.

# create a list with 5 strings
list_of_elements = ["welcome","to","thispointer","python","tutorial"]

print("Indices: ")

# return the  indices  from the list_of_elements
print([idx for idx in range(len(list_of_elements))])

print("Elements: ")

# return the  elements  from the list_of_elements
print([list_of_elements[idx] for idx in range(len(list_of_elements))])

Output:

Indices: 
[0, 1, 2, 3, 4]
Elements: 
['welcome', 'to', 'thispointer', 'python', 'tutorial']

We can see that indices and elements are returned in lists.

Access the index inside for loop using using zip() function

In Python, the zip() function is used to iterate one or more iterables at a time. So It can be possible to iterate the indices and elements at a time using the zip() function.

We have to create two iterables (lists). One is to store indices and the other is to store elements. After that, we can zip these two lists using for loop with zip() function and return the index and element.

Let’s see the syntax.

for index_iterator, element_iterator in zip(list_of_indices,list_of_elements):
    print(index_iterator, element_iterator)

Here,
1. list_of_elements represents the data that contains elements.
2. list_of_indices represents the data that contains the index.
3. index_iterator is a variable used to iterate the elements in a list of index elements and element_iterator is a variable used to iterate the elements in a list of elements inside for loop.

Let’s see the example, to understand it better.

Example:

In this example, we are applying zip() to combine indices and elements inside for loop and access them

# create a list of elements with 5 strings
list_of_elements = ["welcome","to","thispointer","python","tutorial"]

# create a list of elements with 5 indices
list_of_indices = [idx for idx in range(len(list_of_elements))]

# apply zip()
for index_iterator, element_iterator in zip(list_of_indices,list_of_elements):
    print(index_iterator, element_iterator)

Output:

0 welcome
1 to
2 thispointer
3 python
4 tutorial

From the above output, we can see that the index, as well as elements, is returned.

Summary

In the above tutorial, we used four approaches to access the index along with values inside a for loop in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top