0
0
Pythonprogramming~10 mins

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

Choose your learning style9 modes available
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.