Iterate Through a List Using For Loop in Python
In Python, you can iterate through a list using a for
loop to access each element one by one. Through iteration, we can perform operations like printing, modifying, or applying logic to each item in the list.
Examples
1. Iterating Through a List of Numbers
We can use a for
loop to iterate over a list of numbers and print each element.
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Iterating through the list using a for loop
for num in numbers:
print(num)
Here, we have a list named numbers
containing integers. The for
loop iterates over the list, assigning each element to the variable num
, which is then printed in each iteration.
Output:
1
2
3
4
5
2. Iterating Through a List of Strings
Lists can store strings, and we can iterate through them using a for
loop.
# List of fruits
fruits = ["apple", "banana", "cherry"]
# Iterating through the list
for fruit in fruits:
print("I like", fruit)
We have a list called fruits
containing three string elements. The for
loop assigns each element to the variable fruit
and prints a message for each fruit.
Output:
I like apple
I like banana
I like cherry
3. Iterating Through a List Using enumerate()
The enumerate()
function allows us to iterate over a list while keeping track of the index.
# List of colors
colors = ["red", "green", "blue"]
# Iterating using enumerate() to get index and value
for index, color in enumerate(colors):
print(f"Color at index {index} is {color}")
We use enumerate()
to get both the index and value while iterating through the list colors
. The index is assigned to index
and the value to color
, which are printed in each iteration.
Output:
Color at index 0 is red
Color at index 1 is green
Color at index 2 is blue
4. Iterating Through a List Using range()
and len()
We can use range()
with len()
to iterate through a list by index.
# List of countries
countries = ["USA", "Canada", "Germany"]
# Iterating using range() and len()
for i in range(len(countries)):
print(f"Country at index {i}: {countries[i]}")
Here, we use range(len(countries))
to generate index values. The loop variable i
represents the index, which we use to access elements from countries
.
Output:
Country at index 0: USA
Country at index 1: Canada
Country at index 2: Germany
5. Iterating Through a List of Tuples (Unpacking)
If the list contains tuples, we can iterate and unpack values inside the loop.
# List of tuples containing student names and grades
students = [("Alice", 85), ("Bob", 90), ("Charlie", 78)]
# Iterating through list of tuples
for name, grade in students:
print(f"{name} scored {grade} marks")
The list students
contains tuples with names and grades. In each iteration, the for
loop unpacks each tuple into variables name
and grade
to display their values.
Output:
Alice scored 85 marks
Bob scored 90 marks
Charlie scored 78 marks
Conclusion
There are multiple ways to iterate through a list using a for
loop:
- Basic iteration: Access each element directly.
- Iterating through strings: Useful for lists containing words or characters.
- Using
enumerate()
: Retrieves both index and value. - Using
range()
andlen()
: Iterates by index. - Unpacking tuples: Extracts multiple values inside the loop.