← Back to Chapters

Python Loop Lists

? Python Loop Lists

⚡ Quick Overview

Looping through lists is one of the most common tasks in Python. You can iterate over list items directly using a for loop, work with indexes using range() and len(), control the loop with while, or use concise list comprehensions for compact, expressive code.

? Key Concepts

  • Item-based iteration — loop directly over each element in the list.
  • Index-based iteration — use indexes when you need the position of each element.
  • While loops — iterate while a condition is true, manually managing the index.
  • List comprehensions — compact syntax for looping and transforming data into a new list.

? Syntax & Theory

  • for item in list_name: — best for readability when you just need the value.
  • for i in range(len(list_name)): — use when you need both index and value.
  • while condition: — works with a manually updated index variable.
  • [expression for item in list_name] — creates a new list using a loop in one line.

? Code Examples

? Using a for Loop to Iterate Over Items
# Define a list of fruits
fruits = ["apple", "banana", "cherry"]

# Loop through each fruit in the list
for fruit in fruits:
    # Print the current fruit
    print(fruit)
? Looping Through Index Numbers
fruits = ["apple", "banana", "cherry"]

# Use range(len()) to loop through indexes
for i in range(len(fruits)):
    # Print the index and the item at that index
    print(i, fruits[i])
? Using a while Loop with Index
fruits = ["apple", "banana", "cherry"]

# Start with the first index
i = 0

# Continue looping while i is less than the list length
while i < len(fruits):
    # Print the current item
    print(fruits[i])
    # Move to the next index
    i += 1
⚡ Using List Comprehension
fruits = ["apple", "banana", "cherry"]

# Print each fruit in uppercase using a list comprehension
[print(fruit.upper()) for fruit in fruits]

? Sample Output

For the basic for loop example:

apple
banana
cherry

The loop takes each element from the fruits list in order and prints it. When using index-based loops, you also get access to the position of each element, which is useful for tasks like displaying numbered lists or modifying elements in place. List comprehensions evaluate an expression for every element and can build a new list or trigger actions for each item.

✅ Tips & Best Practices

  • Prefer for loops for clean, readable iteration over list items.
  • Use while loops when you need more complex conditions or to stop in the middle of a list.
  • Use list comprehensions for concise transformations and filtering of lists.
  • Always increment your index in a while loop to avoid infinite loops.
  • Be careful with range() boundaries to avoid off-by-one errors.
  • Avoid very complex list comprehensions; if they get hard to read, switch back to a normal loop.

? Practice Tasks

  • Create a list of numbers and use a for loop to print only the even numbers.
  • Use a for loop with range() to print list items along with their index.
  • Iterate over a list with a while loop and stop when a specific value is found.
  • Write a list comprehension that creates a new list containing the squares of numbers from 1 to 10.