Skip to content
← Python for AI/ML · all 50 levels

List comprehensions

Level 31 of 50 · Data structures

Level 31 of 50

List comprehensions

explained with grace marks. Watch the reel, then work through it below: run the code, take the quiz, keep the cheat sheet.

📒 Open the cheat sheet
SanaSir gave everyone 5 grace marks! Yay!
TeacherAdd 5 to every mark? One line! A list comprehension!
marks.py
marks = [40, 72, 55]
new = [m + 5 for m in marks]
print(new)
TeacherSquare brackets. The new value first, then the loop!
SanaHow do I even READ that?
TeacherOut loud! m + 5, for every m in marks!
marks.py
ms = [40, 72, 55]
ok = [m for m in ms if m >= 50]
print(ok)
TeacherAdd an if, and it filters too! Only the passes stay!

⏸ Pause & predict: what prints?

nums = [1, 2, 3, 4]
big = [n * n for n in nums
       if n > 2]
print(big)

Where this leads

Python is step one. Building AI that ships is the rest.

The diploma takes you from zero to deployed computer vision, LLM and agent projects, with engineers who build them for a living. Already code? The bootcamp does it in three months.