Python Academy · Lesson

List Comprehensions

Create lists concisely using comprehension syntax with conditions.

Lesson 1 of 414 steps

List Comprehensions is a free Python Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

Introduction

List comprehensions create new lists from iterables in a single readable expression, replacing common for-loop patterns.

Basic Syntax

[expression for item in iterable] is the core form. [x*2 for x in range(5)] gives [0,2,4,6,8].
doubled = [x*2 for x in range(5)]
print(doubled)

With a Condition

[x for x in range(10) if x%2==0] filters to even numbers. The if clause comes after the for clause.
evens = [x for x in range(10) if x % 2 == 0]
print(evens)

Transforming Strings

[word.upper() for word in words] applies upper() to every string. Works on any iterable.
words = ['hello', 'world']
print([w.upper() for w in words])

Nested Comprehension

[x for row in matrix for x in row] is a flat nested comprehension (reads left-to-right, outer loop first).
matrix = [[1,2,3],[4,5,6]]
flat = [x for row in matrix for x in row]
print(flat)

if/else in Expression

[x if x>0 else 0 for x in lst] uses the ternary in the expression part (before for), not after.
lst = [-1, 2, -3, 4]
clipped = [x if x > 0 else 0 for x in lst]
print(clipped)

Comprehension vs map/filter

[f(x) for x in it if cond(x)] is equivalent to list(map(f, filter(cond, it))) but more readable.
nums = range(10)
result = [x**2 for x in nums if x%2==0]
print(result)

Flattening with Comprehension

For 2+ levels of nesting, a comprehension can flatten: [[1,2],[3,4]] → [1,2,3,4].
nested = [[1,2],[3,4],[5]]
flat = [x for sub in nested for x in sub]
print(flat)

Comprehension with enumerate()

[(i, x) for i, x in enumerate(lst)] gives a list of index-value pairs.
colors = ['red', 'green', 'blue']
indexed = [(i, c) for i, c in enumerate(colors)]
print(indexed)

Comprehension with zip()

[a+b for a, b in zip(lst1, lst2)] adds corresponding elements of two lists.
a = [1, 2, 3]
b = [10, 20, 30]
print([x+y for x,y in zip(a,b)])

When NOT to Use Comprehensions

If the logic is complex or needs side effects, use a regular for loop. Comprehensions should remain one-liners.
# Too complex for comprehension:
result = []
for x in range(10):
    if x > 5:
        result.append(x * x)
print(result)

Quick Check

In a list comprehension with both a condition and a ternary, where does the ternary go?

Recap

List comprehensions: [expr for x in it if cond]. Ternary in expression: [x if cond else y for x in it]. Use for flat, readable transformations.

Keep Going

Excellent! Continue to the next lesson to deepen your skills.
Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “List Comprehensions” lesson free?

Yes — the full text of “List Comprehensions” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.

What will I learn in “List Comprehensions”?

Create lists concisely using comprehension syntax with conditions. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “List Comprehensions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. List Comprehensions
  2. Set and Dict Comprehensions
  3. Generator Expressions
  4. The yield Keyword
← Back to Python Academy