Theory

List comprehensions create lists in one line.

Basic syntax:
[expression for item in iterable]

Examples:

# Create list of squares
squares = [x * x for x in range(5)]
# Result: [0, 1, 4, 9, 16]

# Double each number
numbers = [1, 2, 3]
doubled = [n * 2 for n in numbers]
# Result: [2, 4, 6]

# With condition (filter)
even = [x for x in range(10) if x % 2 == 0]
# Result: [0, 2, 4, 6, 8]

Exercises in this lesson

3

Filter Even Numbers

Create a list of even numbers from 1 to 10. Use list comprehension with if condition. Result: [2, 4, 6, 8, 10] Print the list.

Medium Points: 20
4

Transform List

Given: words = ["hello", "world", "python"] Create a new list with all words in uppercase. Use list comprehension with .upper() Result: ["HELLO", "WORLD"…

Medium Points: 20
Solve in the playground

Frequently asked questions

What is the lesson “List Comprehensions” about?

Learn to create lists in one line using list comprehensions.

How many exercises are in this lesson?

This lesson has 4 exercises.

Do I need to install anything to do the exercises?

No. The code is written and run in the browser.

Next lesson: F-strings →