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.
# 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 infruits
. - In the For Loop,
index
holds the position (starting from 0), andfruit
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.
# 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 from0
tolen(animals) - 1
.i
represents the index, andanimals[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.
# 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.
# 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, andstudent
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:
enumerate()
: The most Pythonic way to access both index and value.range(len(list))
: Useful when modifying elements inside a loop.- List Comprehension with
enumerate()
: Ideal for generating new lists with indexed values. zip()
withrange()
: An alternative approach for pairing indexes and values.