Python Program - Access Index of a List Using for Loop

To access the index of a list using a for loop in Python, you can use the enumerate() function. Here's an example program:

fruits = ["apple", "banana", "cherry"]

for index, fruit in enumerate(fruits):
    print("Index:", index, "Fruit:", fruit)
So‮ecru‬:www.theitroad.com

In this program, the enumerate() function is used to loop over the fruits list and retrieve both the index and the value of each element. The index variable will contain the current index of the element being processed, and the fruit variable will contain the value of the element itself. The print() function is then used to display the index and value of each element.

When you run this program, the output will be:

Index: 0 Fruit: apple
Index: 1 Fruit: banana
Index: 2 Fruit: cherry