Bird
Raised Fist0
Pythonprogramming~10 mins

Iterating over lists in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Iterating over lists
Start with list
Set index i = 0
Check: i < length of list?
NoExit loop
Yes
Access list[i
Execute loop body
Increment i by 1
Back to Check
This flow shows how we start from the first item in the list, check if we still have items left, process the current item, then move to the next until all items are done.
Execution Sample
Python
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)
This code goes through each fruit in the list and prints its name one by one.
Execution Table
IterationVariable 'fruit'ActionOutput
1'apple'Access fruits[0]apple
2'banana'Access fruits[1]banana
3'cherry'Access fruits[2]cherry
--No more items, loop ends-
💡 After 3 iterations, all items are processed, loop stops.
Variable Tracker
VariableStartAfter 1After 2After 3Final
fruitundefined'apple''banana''cherry''cherry'
Key Moments - 3 Insights
Why does the loop stop after the last item?
The loop checks if there are more items by comparing the current index with the list length. When the index reaches the list length (3), the condition fails and the loop ends, as shown in the last row of the execution_table.
Is the variable 'fruit' still available after the loop?
After the loop ends, 'fruit' holds the last item value ('cherry') until overwritten or the program ends, as shown in variable_tracker final column.
What if the list is empty?
If the list is empty, the loop body never runs because the condition to start the loop is false immediately. So no output is produced and 'fruit' is never assigned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'fruit' during iteration 2?
A'cherry'
B'banana'
C'apple'
Dundefined
💡 Hint
Check the 'Variable fruit' column in the execution_table row for iteration 2.
At which iteration does the loop stop according to the execution_table?
AAfter iteration 2
BAfter iteration 1
CAfter iteration 3
DIt never stops
💡 Hint
Look at the exit_note and the last row of the execution_table.
If the list was empty, what would the output be?
ANo output
BError message
CPrints 'undefined'
DPrints the last item
💡 Hint
Refer to the key_moments explanation about empty lists.
Concept Snapshot
Iterating over lists in Python:
Use 'for item in list:' to go through each element.
The loop runs once per item, from first to last.
Inside the loop, 'item' holds the current element.
Loop ends when all items are processed.
If list is empty, loop body does not run.
Full Transcript
This lesson shows how to go through each item in a list using a for loop in Python. We start with the first item, assign it to a variable, run the loop body, then move to the next item until all are done. The execution table tracks each step, showing the variable's value and output. Key points include understanding when the loop stops, what happens if the list is empty, and the variable's state after the loop. The quiz tests your understanding by asking about variable values during iterations and loop behavior with empty lists.

Practice

(1/5)
1. What does the following code do?
for item in [1, 2, 3]:
    print(item)
easy
A. Prints the whole list once
B. Prints numbers 1 to 3 without spaces
C. Prints each number 1, 2, and 3 on a new line
D. Causes an error because of the loop

Solution

  1. Step 1: Understand the for loop over a list

    The loop goes through each element in the list [1, 2, 3] one by one.
  2. Step 2: Print each element during iteration

    Each element (1, then 2, then 3) is printed on its own line because of the print statement.
  3. Final Answer:

    Prints each number 1, 2, and 3 on a new line -> Option C
  4. Quick Check:

    Loop prints items one by one [OK]
Hint: For loops print each list item separately [OK]
Common Mistakes:
  • Thinking it prints the whole list at once
  • Expecting numbers to print on the same line
  • Confusing loop variable with list itself
2. Which of these is the correct syntax to loop over a list named fruits?
easy
A. for fruit in fruits: print(fruit)
B. for fruits in fruit: print(fruits)
C. loop fruit in fruits: print(fruit)
D. for fruit to fruits: print(fruit)

Solution

  1. Step 1: Identify correct for loop syntax

    The correct Python syntax uses 'for variable in list:' to loop over items.
  2. Step 2: Check each option

    for fruit in fruits: print(fruit) uses 'for fruit in fruits:', which is correct. Others use invalid keywords or reversed names.
  3. Final Answer:

    for fruit in fruits: print(fruit) -> Option A
  4. Quick Check:

    Use 'for item in list:' syntax [OK]
Hint: Remember: for variable in list: [OK]
Common Mistakes:
  • Swapping variable and list names
  • Using wrong keywords like 'loop' or 'to'
  • Missing colon after for statement
3. What is the output of this code?
numbers = [2, 4, 6]
sum = 0
for n in numbers:
    sum += n
print(sum)
medium
A. 12
B. 0
C. 246
D. Error

Solution

  1. Step 1: Understand the loop adding numbers

    The loop adds each number in the list [2, 4, 6] to sum, starting from 0.
  2. Step 2: Calculate the total sum

    sum = 0 + 2 + 4 + 6 = 12
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    Sum of list items = 12 [OK]
Hint: Add each item inside the loop to get total [OK]
Common Mistakes:
  • Printing sum before loop
  • Concatenating numbers as strings
  • Forgetting to initialize sum to 0
4. Find the error in this code:
items = [10, 20, 30]
for i in items
    print(i)
medium
A. List name is incorrect
B. Wrong variable name in loop
C. Indentation error in print
D. Missing colon after for statement

Solution

  1. Step 1: Check for syntax errors in for loop

    The for loop must end with a colon ':' to be valid syntax.
  2. Step 2: Identify missing colon

    The code misses ':' after 'for i in items', causing a syntax error.
  3. Final Answer:

    Missing colon after for statement -> Option D
  4. Quick Check:

    For loops need ':' at end [OK]
Hint: Always put ':' after for loop header [OK]
Common Mistakes:
  • Forgetting colon after for statement
  • Using wrong variable names
  • Incorrect indentation of print
5. You have a list of lists: data = [[1, 2], [3, 4], [5, 6]]. How do you print all numbers one by one using iteration?
hard
A. print(data[0][0], data[1][1], data[2][2])
B. for sublist in data: for num in sublist: print(num)
C. for i in range(len(data)): print(data[i])
D. for num in data: print(num)

Solution

  1. Step 1: Understand nested lists and iteration

    Each item in data is a list itself, so we need two loops: one for each sublist, one for numbers inside.
  2. Step 2: Use nested for loops to access all numbers

    The outer loop goes through each sublist, the inner loop prints each number inside that sublist.
  3. Final Answer:

    for sublist in data: for num in sublist: print(num) -> Option B
  4. Quick Check:

    Nested loops print all inner items [OK]
Hint: Use nested loops for lists inside lists [OK]
Common Mistakes:
  • Trying to print sublists directly
  • Using single loop only
  • Accessing wrong indexes causing errors