Write a Python Program to Access Index of a List Using for Loop

Python is a high-level, interpreted programming language that is widely used for various purposes, including data analysis, machine learning, and web development.

One of the most common tasks in Python programming is accessing the index of a list using a for loop.

In this tutorial, we will show you how to do it with a simple Python program.


A list is an ordered collection of items, and each item has an index associated with it.

The index starts at 0 and increments by 1 for each item in the list.

To access the index of a list using a for loop, we can use the built-in enumerate() function in Python.

The enumerate() function takes a list as an argument and returns an object that contains tuples of the form (index, item) for each item in the list.

Here is an example Python program that accesses the index of a list using a for loop and the enumerate() function:

fruits = ["apple", "banana", "cherry", "date", "elderberry"]
for index, fruit in enumerate(fruits):
    print("The index of", fruit, "is", index)

Output:

The index of apple is 0
The index of banana is 1
The index of cherry is 2
The index of date is 3
The index of elderberry is 4

In this program, we define a list of fruits and use a for loop to iterate over the list.

Inside the loop, we use the enumerate() function to get the index and item for each element in the list.

Then, we print a message that shows the index of each fruit.


In conclusion, accessing the index of a list using a for loop in Python is a straightforward task that can be accomplished with the help of the enumerate() function.

This simple Python program can be used as a starting point for more complex programs that involve lists and loops.