Python Lists – Iterating a list using for loop (Get item and index)

In this post we will be iterating over a list using a for loop, whilst do so we will be retrieving both the items and their index.

See below our list which consists of six items.

List = ['Ali', 'Bella', 'Christina', 'Danny', 'Eli', 'Farooq']

See the snippet of code below that will iterate the list along with each items index.

for index, item in enumerate(List):
    print(index, item)

The above will output the following.

0 Ali
1 Bella
2 Christina
3 Danny
4 Eli
5 Farooq

You will notice that the index naturally begins at 0, if you are wanting to change the start position of the list, refer to the below.

for index, item in enumerate(List, start=1):
    print(index, item)

The above will output the following instead.

1 Ali
2 Bella
3 Christina
4 Danny
5 Eli
6 Farooq

Leave a Reply