Collections · Lesson 16 of 25
Comprehensions, enumerate and zip
Transform, filter and pair collections.
8 minutes2 games
A comprehension builds a new collection: [expression for item in source if condition]. enumerate provides an index and item together; zip pairs corresponding items from iterables.
Python
nums = [1, 2, 3, 4]
print([n * n for n in nums if n % 2 == 0])
for index, name in enumerate(["Ali", "Mona"], start=1):
print(index, name)
print(dict(zip(["Ali", "Mona"], [80, 95])))Each item
Filter condition
Compute expression
New collection
What's the output?
print([n + 1 for n in [1, 3] if n > 1])
What will this code print?
Exercise
Print squares of only the even numbers.
Expected output: [4, 16, 36]
Python
nums = [1, 2, 3, 4, 5, 6]
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.