Challenge - 5 Problems
Length and Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of length and iteration over a nested list
What is the output of this Python code?
Python
nested = [[1, 2], [3, 4, 5], []] count = 0 for sublist in nested: count += len(sublist) print(count)
Attempts:
2 left
💡 Hint
Count the total number of elements inside all sublists.
✗ Incorrect
The code sums the lengths of each sublist: 2 + 3 + 0 = 5.
❓ Predict Output
intermediate2:00remaining
Length of a dictionary after iteration
What is the output of this code snippet?
Python
d = {'a': 1, 'b': 2, 'c': 3}
for key in d:
d[key] += 1
print(len(d))Attempts:
2 left
💡 Hint
Length of a dictionary counts keys, not values.
✗ Incorrect
The dictionary has 3 keys, so len(d) is 3 regardless of value changes.
❓ Predict Output
advanced2:00remaining
Output of iteration with modification inside loop
What is the output of this code?
Python
lst = [1, 2, 3, 4] for i in lst: lst.remove(i) print(lst)
Attempts:
2 left
💡 Hint
Removing items while iterating changes the list length and indices.
✗ Incorrect
Removing elements while iterating skips some elements. The resulting list is [2, 4].
❓ Predict Output
advanced2:00remaining
Length and iteration over a generator expression
What happens when you run this code?
Python
gen = (x for x in range(5)) print(len(gen))
Attempts:
2 left
💡 Hint
Generators do not support len() because they produce items on the fly.
✗ Incorrect
Calling len() on a generator raises TypeError because generators have no length.
❓ Predict Output
expert3:00remaining
Final value after complex iteration and length checks
What is the value of variable 'result' after running this code?
Python
data = ['a', 'bb', 'ccc', 'dddd'] result = 0 for item in data: if len(item) % 2 == 0: result += len(item) else: result -= 1 print(result)
Attempts:
2 left
💡 Hint
Add lengths of even-length strings, subtract 1 for odd-length strings.
✗ Incorrect
Lengths: 'a'(1 odd) -> -1, 'bb'(2 even) -> +2, 'ccc'(3 odd) -> -1, 'dddd'(4 even) -> +4; total = -1+2-1+4=4