Python · live now

Comprehensions

Build a collection from an existing one in a single expression: list comprehensions with and without a condition, and the set and dictionary forms.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

5

lessons

24

practice questions

64

prerequisite lessons

See it taught

1 of the 5 lessons here is free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3
nums = [1, 2, 3, 4] squares = [n * n for n in nums] print(squares[-2:])

Editor Output

1

Output:

[9, 16]

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 5 lessons

The 5 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

List comprehensions

Preview

8 questions

Learn how to use list comprehensions in Python to construct new lists by applying an expression to each item in an iterable in a concise and readable way.

Covers

Overview of list comprehensions

Basic list comprehensions

Preview this lesson - no account needed

02

List comprehensions with conditional logic

6 questions

Learn how to use list comprehensions with if and if-else statements in Python to filter elements or apply different logic to each item based on a condition.

Covers

List comprehensions with an if statement

List comprehensions with an if-else statement

03

Set comprehensions

3 questions

See how to use set comprehensions in Python to generate sets from iterables with optional filtering and transformation, producing collections of unique values.

Covers

Set comprehensions

04

Dictionary comprehensions

3 questions

Learn how to construct dictionaries concisely using dictionary comprehensions with optional filtering and key or value transformations based on iterables or existing dictionaries.

Covers

Dictionary comprehensions

05

Activity: zombie apocalypse

4 questions

Apply programming techniques to solve practical problems by writing Python code that locates safe zones, distributes supplies, constructs barricades, and plans evacuation routes in a simulated zombie apocalypse scenario.

Covers

Locate safe zones

Distribute supplies

Build defensive barricades

Plan evacuation routes

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 5 red dots are the lessons in Comprehensions; the 64 blue dots feeding into them are the lessons Comprehensions depends on; the 31 pale dots are the rest of the learning path, which come after Comprehensions or alongside it.ComprehensionsFirst lessons on the left100 lessons

Hover any dot to name its lesson.

5 lessons in this unit

64 prerequisite lessons across the unit, counting every step back to the start

31 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start Comprehensions

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee

More in Iteration: Sequences, Loops