Loop Lists

Looping through lists is a fundamental Python skill. It allows you to read, process, modify, and analyze each item in a list efficiently.
Python provides multiple ways to loop through lists, each useful in different situations.

Loop Through a List Using for

The most common and readable way to loop through a list.
python
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
    print(fruit)
python
numbers = [1, 2, 3, 4]
for num in numbers:
    print(num)

Loop Using Index Numbers

Use this when you need the index position.
python
colors = ["red", "green", "blue"]

for i in range(len(colors)):
    print(colors[i])
python
for i in range(len(colors)):
    print(i, colors[i])

Loop Using while

The while loop is useful when you want more control over iteration.
python
numbers = [10, 20, 30]
i = 0

while i < len(numbers):
    print(numbers[i])
    i += 1

Loop Using enumerate() (Recommended)

enumerate() gives both index and value.
python
fruits = ["apple", "banana", "cherry"]

for index, value in enumerate(fruits):
    print(index, value)
python
for index, value in enumerate(fruits, start=1):
    print(index, value)

Loop Using List Comprehension

List comprehensions are compact and Pythonic.
python
numbers = [1, 2, 3, 4]
squares = [x * x for x in numbers]
print(squares)
python
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)

Loop Through Nested Lists

python
matrix = [
    [1, 2],
    [3, 4],
    [5, 6]
]

for row in matrix:
    for value in row:
        print(value)

Loop and Modify List Items

Use index-based looping when modifying.
python
numbers = [1, 2, 3]

for i in range(len(numbers)):
    numbers[i] *= 10

print(numbers)

Loop with Conditions

python
numbers = [10, 15, 20, 25]

for n in numbers:
    if n > 15:
        print(n)

Loop with break

Stops the loop when a condition is met.
python
numbers = [1, 2, 3, 4, 5]

for n in numbers:
    if n == 3:
        break
    print(n)

Loop with continue

Skips the current iteration.
python
numbers = [1, 2, 3, 4, 5]

for n in numbers:
    if n == 3:
        continue
    print(n)

Loop Using zip() (Advanced)

Loop through multiple lists together.
python
names = ["A", "B", "C"]
scores = [80, 90, 85]

for name, score in zip(names, scores):
    print(name, score)

Common Mistakes

Modifying List While Looping Directly

python
nums = [1, 2, 3, 4]

for n in nums:
    if n % 2 == 0:
        nums.remove(n)

print(nums)
This causes unexpected behavior.

Safe Way to Modify While Looping

python
nums = [1, 2, 3, 4]
nums = [n for n in nums if n % 2 != 0]
print(nums)

Performance Tip

  • Use for loop for readability
  • Use enumerate() when index is needed
  • Use list comprehension for transformation
  • Avoid unnecessary index loops

Summary

  • Python offers multiple ways to loop through lists
  • for loop is most common
  • while loop gives manual control
  • enumerate() provides index and value
  • Nested loops handle nested lists
  • Be careful when modifying lists while looping