Basic List Comprehension
Mon 10 August 2026
squares = [x**2 for x in range(5)]
squares = []
for x in range(5):
squares.append(x**2)
Score: 15
Category: chapter9_list_comprehension
squares = [x**2 for x in range(5)]
squares = []
for x in range(5):
squares.append(x**2)
Score: 15
Category: chapter9_list_comprehension
even_numbers = [x for x in range(10) if x % 2 == 0]
Score: 10
Category: chapter9_list_comprehension
Read Morekeys = ["a", "b"]
values = [1, 2]
data = {k: v for k, v in zip(keys, values)}
Score: 10
Category: chapter9_list_comprehension
Read Morenested = [[1, 2], [3, 4], [5]]
flat = [item for sublist in nested for item in sublist]
Score: 10
Category: chapter9_list_comprehension
Read Morestatus = ["even" if x % 2 == 0 else "odd" for x in range(5)]
Score: 10
Category: chapter9_list_comprehension
Read Moredef transform(x):
return x * 10
result = [transform(x) for x in range(5)]
Score: 10
Category: chapter9_list_comprehension
Read Moreresult = [x for x in range(20) if x > 5 if x % 2 == 0]
Score: 10
Category: chapter9_list_comprehension
Read Morematrix = [[i * j for j in range(3)] for i in range(3)]
Score: 10
Category: chapter9_list_comprehension
Read More