How to Access Index in Python’s for Loop

python_tutorials

Introduction

Python is a very high-level programming language, and it tends to stray away from anything remotely resembling internal data structure. Because of this, we usually don’t really need indices of a list to access its elements, however, sometimes we desperately need them.

In this article, we will go over different approaches on how to access an index in Python’s for loop.

How to Access Index in Python’s for Loop?

The easiest, and most popular method to access the index of elements in a for loop is to go through the list’s length, increasing the index. On each increase, we access the list on that index:

my_list = [3, 5, 4, 2, 2, 5, 5]

print("Indices and values in my_list:")

for index in range(len(my_list)):
    print(index, my_list[index], end = "n")

Here, we don’t iterate through the list, like we’d usually do. We iterate from 0..len(my_list) with

 

 

To finish reading, please visit source site