Iterating over lists in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we go through each item in a list one by one, it takes some time. We want to understand how this time changes when the list gets bigger.
The question is: How does the time to look at every item grow as the list grows?
Analyze the time complexity of the following code snippet.
def print_items(items):
for item in items:
print(item)
my_list = [1, 2, 3, 4, 5]
print_items(my_list)
This code goes through each item in a list and prints it out.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list.
- How many times: Once for every item in the list.
As the list gets bigger, the number of times we print grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The number of steps grows directly with the size of the list.
Time Complexity: O(n)
This means if the list doubles in size, the time to go through it also doubles.
[X] Wrong: "Looping through a list always takes the same time no matter how big the list is."
[OK] Correct: The time depends on how many items are in the list. More items mean more steps.
Understanding how looping through lists grows with size helps you explain your code clearly and shows you know how programs handle data efficiently.
"What if we nested one loop inside another to go through the list twice? How would the time complexity change?"
Practice
for item in [1, 2, 3]:
print(item)Solution
Step 1: Understand the for loop over a list
The loop goes through each element in the list [1, 2, 3] one by one.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.Final Answer:
Prints each number 1, 2, and 3 on a new line -> Option CQuick Check:
Loop prints items one by one [OK]
- Thinking it prints the whole list at once
- Expecting numbers to print on the same line
- Confusing loop variable with list itself
fruits?Solution
Step 1: Identify correct for loop syntax
The correct Python syntax uses 'for variable in list:' to loop over items.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.Final Answer:
for fruit in fruits: print(fruit) -> Option AQuick Check:
Use 'for item in list:' syntax [OK]
- Swapping variable and list names
- Using wrong keywords like 'loop' or 'to'
- Missing colon after for statement
numbers = [2, 4, 6]
sum = 0
for n in numbers:
sum += n
print(sum)Solution
Step 1: Understand the loop adding numbers
The loop adds each number in the list [2, 4, 6] to sum, starting from 0.Step 2: Calculate the total sum
sum = 0 + 2 + 4 + 6 = 12Final Answer:
12 -> Option AQuick Check:
Sum of list items = 12 [OK]
- Printing sum before loop
- Concatenating numbers as strings
- Forgetting to initialize sum to 0
items = [10, 20, 30]
for i in items
print(i)Solution
Step 1: Check for syntax errors in for loop
The for loop must end with a colon ':' to be valid syntax.Step 2: Identify missing colon
The code misses ':' after 'for i in items', causing a syntax error.Final Answer:
Missing colon after for statement -> Option DQuick Check:
For loops need ':' at end [OK]
- Forgetting colon after for statement
- Using wrong variable names
- Incorrect indentation of print
data = [[1, 2], [3, 4], [5, 6]]. How do you print all numbers one by one using iteration?Solution
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.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.Final Answer:
for sublist in data: for num in sublist: print(num) -> Option BQuick Check:
Nested loops print all inner items [OK]
- Trying to print sublists directly
- Using single loop only
- Accessing wrong indexes causing errors
