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 path5
lessons
24
practice questions
64
prerequisite lessons
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
Editor Output
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 in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.
01
List comprehensions
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
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
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.
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
Builds on: Built-in functions, Dictionaries, Sequences, Sets
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