BeginnerCoder

Lesson 17 — List comprehensions

Build lists in one line

A list comprehension creates a new list from an existing sequence in one expression: [expression for x in items]. Add an if at the end to keep only some items. It does the same job as a for loop with append(), just shorter.

words = ["hi", "hello", "yo"]
long_words = [w.upper() for w in words if len(w) > 2]
print(long_words)  # ['HELLO']

Why it matters: comprehensions are everywhere in real Python code, so reading and writing them is a core skill.

Want it another way?
Task

Build a second list with the squares of only the even numbers 1..10 and print it.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Python Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.