Iterate Over a List with Index in Python

To iterate over a list with index in Python, we can use methods like enumerate(), the range() function, list comprehension, or zip(). These approaches allow accessing both the index and the corresponding value of each element in the list during iteration.


Examples

1. Iterate Over a List using enumerate() Function

The enumerate() function provides a built-in way to loop through a list while keeping track of the index.

</>
Copy
# List of fruits
fruits = ["apple", "banana", "cherry"]

# Iterating using enumerate()
for index, fruit in enumerate(fruits):
    print(f"Index {index}: {fruit}")

Explanation:

  • The enumerate() function returns both the index and the value for each element in fruits.
  • In the For Loop, index holds the position (starting from 0), and fruit holds the corresponding element.
  • The print() function outputs the index and value in a formatted string.

Output:

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

2. Iterate Over a List using range() and len()

The range() function with len() allows iterating over a list while manually accessing indexes.

</>
Copy
# List of animals
animals = ["dog", "cat", "rabbit"]

# Iterating using range() and len()
for i in range(len(animals)):
    print(f"Index {i}: {animals[i]}")

Explanation:

  • range(len(animals)) generates indexes from 0 to len(animals) - 1.
  • i represents the index, and animals[i] retrieves the corresponding element.
  • The print() function displays both the index and value.

Output:

Index 0: dog
Index 1: cat
Index 2: rabbit

3. Iterate Over a List using List Comprehension with enumerate()

List comprehension combined with enumerate() allows creating a list of indexed values efficiently.

</>
Copy
# List of colors
colors = ["red", "green", "blue"]

# Creating indexed output using list comprehension
indexed_colors = [f"Index {i}: {color}" for i, color in enumerate(colors)]

# Printing each formatted string
for item in indexed_colors:
    print(item)

Explanation:

  • enumerate(colors) provides indexes and values.
  • List comprehension constructs formatted strings for each element.
  • The final loop prints each formatted string.

Output:

Index 0: red
Index 1: green
Index 2: blue

4. Iterate Over a List using zip() with range()

The zip() function can combine range() and a list to provide index-value pairs.

</>
Copy
# List of students
students = ["Alice", "Bob", "Charlie"]

# Using zip() with range()
for i, student in zip(range(len(students)), students):
    print(f"Index {i}: {student}")

Explanation:

  • zip(range(len(students)), students) creates pairs of indexes and values.
  • i holds the index, and student holds the corresponding name.
  • The loop prints each index-student pair.

Output:

Index 0: Alice
Index 1: Bob
Index 2: Charlie

Conclusion

Python offers multiple ways to iterate over a list while accessing the index:

  1. enumerate(): The most Pythonic way to access both index and value.
  2. range(len(list)): Useful when modifying elements inside a loop.
  3. List Comprehension with enumerate(): Ideal for generating new lists with indexed values.
  4. zip() with range(): An alternative approach for pairing indexes and values.