Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 01

Python Comprehensions — List, Set, Dict, and Generator Overview

Write elegant one-line list, set, and dict comprehensions, split delimited text into collections, and see why parentheses create a generator.

Intermediate25 min
Python comprehensions module — list set dict generator

What you will learn

  • Why Python comprehensions replace long for loops
  • The shared syntax: expression, for name, in iterable, optional if
  • List, set, and dict comprehensions
  • Why (...) is a generator, not a tuple comprehension
  • The split() pattern used to turn delimited text into a collection

Python is famous for code that is elegant, easy to write, and almost as easy to read as plain English. List comprehension is one of the most distinctive features — powerful work in a single line instead of legacy loop code.

This continues from Special Functions (filter / map / reduce) and Collections.

Why comprehensions

Traditional loop versus one-line comprehension
Style Shape
Traditional Create empty collection, loop, append / assign
Comprehension One expression that builds the collection

The teaching notes call this the most effective way to read and transform list data compared with traditional reading.

Shared syntax

python
result = [expression for varname in iterable if condition]
  • expression — type casting (int(val), float(val)) or math (val * 2)
  • varname — each item from the iterable
  • iterable — list, string tokens from split(), range(), and similar
  • if condition — optional filter; omit it to keep every item

if is not required. Brackets choose the collection type.

Four forms

List set dict and generator parentheses
Form Brackets Result
List [...] list — order and duplicates kept
Set {...} with one expression set — unique values
Dict {key: value ...} dict — unique keys
Generator (...) generator — convert with tuple()

There is no true tuple comprehension. Parentheses around a for create a generator object.

Delimited text

Classroom examples often use input().split() so a learner types values separated by space, comma, or another delimiter.

The playground cannot wait on input(). Use the same idea with a string and .split():

python
raw = "10 2 22 50 -3 0 22"
lst = [float(val) for val in raw.split()]
print(lst)

Change the string and re-run — that is the same comprehension as the keyboard version.

Learning path

Five lesson path from overview to generator
# Lesson Focus Time
2 List comprehension Cast, math, if filter 40 min
3 Set comprehension Unique values 25 min
4 Dict comprehension Squares and word lengths 35 min
5 Tuple and generator (...) then tuple() 25 min

Common mistakes

Using ( ) and expecting a tuple — you get a generator until you call tuple(...).

Forgetting the expression — [for x in nums] is invalid; you need [x for x in nums] or [x * 2 for x in nums].

Putting if in the wrong place — the filter comes after for ... in ....

Interview cheat sheet

Question Answer
One-line list build? List comprehension
Unique values? Set comprehension
Key to value? Dict comprehension
(x for x in data) type? generator
How to get a tuple? tuple(generator)
Optional clause? if
List set dict generator memory row

Practice in the playground

The starter already runs list, set, dict, and generator forms. Try:

  1. Change raw to "1 2 3" and confirm the list becomes floats.
  2. Change the set source to "5,5,5" — the set should have one 5.
  3. Print gen before tuple(gen) — you should see a generator object.

Next: List comprehension.

Loading playground…

Check your understanding

Comprehensions overview quiz

What is the main purpose of a comprehension in Python?

1 / 10