Python List Comprehension

Let’s look at list comprehension

  1. Notice

    A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)
Live state

Top-level scope

numbers→Object 1
squares→Object 7
Object 1 · rangerange(0, 5)Object 7 · list[0, 4, 16]
This step

A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.

Picture list comprehension

A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.

Image brief

Alt text
A diagram for Python List Comprehension. Instructions: numbers = range(5) squares = [number * number for number in numbers if number % 2 == 0] print(squares). Displayed result: [0, 4, 16]. The final example bindings are numbers --> object 1 (range); squares --> object 7 (list). object 1: range(0, 5); object 7: [0, 4, 16]
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python List Comprehension". Teaching focus: A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS numbers = range(5) squares = [number * number for number in numbers if number % 2 == 0] print(squares) | v DISPLAYED RESULT [0, 4, 16] FINAL BINDINGS numbers --> object 1 (range) squares --> object 7 (list) object 1: range(0, 5) object 7: [0, 4, 16] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python List Comprehension. Instructions: numbers = range(5) squares = [number * number for number in numbers if number % 2 == 0] print(squares). Displayed result: [0, 4, 16]. The final example bindings are numbers --> object 1 (range); squares --> object 7 (list). object 1: range(0, 5); object 7: [0, 4, 16]

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)
Live state

Top-level scope

numbers→Object 1
squares→Object 7
Object 1 · rangerange(0, 5)Object 7 · list[0, 4, 16]
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[0, 4, 16]

What do you expect this program to print?

What does this example display?

numbers = range(5)
squares = [number * number for number in numbers if number % 2 == 0]
print(squares)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[0, 4, 16]

Follow the printed values in order. The output is: [0, 4, 16]

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.

A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.

More examples and reference

In Python, there's a short and neat way to create new lists. It’s called list comprehension. It looks a little different from what you've seen before, but don't worry — you'll understand it step by step.

What is List Comprehension?

List comprehension is a way to create a new list by doing something to each item in another list (or a range of numbers).

Instead of writing a loop and appending to a list, you can write it all in one line.

Normal Way vs List Comprehension

Let’s first see how we normally make a new list from numbers 1 to 5:

Using a for loop:

numbers = []
for i in range(1, 6):
    numbers.append(i)
print(numbers)
[1, 2, 3, 4, 5]

Now, using list comprehension:

numbers = [i for i in range(1, 6)]
print(numbers)
[1, 2, 3, 4, 5]

Explanation:

  • i for i in range(1, 6) means: “for every number i from 1 to 5, take i and put it into the list.”
  • The square brackets [ ] mean we’re making a list.

Example 2: Square Each Number

squares = [i*i for i in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

Why? Because:

  • When i = 1 → 1*1 = 1
  • When i = 2 → 2*2 = 4
  • ... and so on

Example 3: Double Only Even Numbers

doubles = [i*2 for i in range(1, 6) if i % 2 == 0]
print(doubles)
[4, 8]

Why?

  • i % 2 == 0 checks if a number is even
  • Only 2 and 4 are even between 1 to 5
  • So we double only those: 2→4, 4→8

Example 4: Convert to Uppercase

words = ["apple", "banana", "cherry"]
uppercased = [word.upper() for word in words]
print(uppercased)
['APPLE', 'BANANA', 'CHERRY']

Why?

  • word.upper() turns each word into uppercase.
  • Each word from the list is transformed and put into a new list.

Example 5: Filter Words Starting with "b"

words = ["apple", "banana", "cherry", "blueberry"]
starts_with_b = [word for word in words if word.startswith("b")]
print(starts_with_b)
['banana', 'blueberry']

Why?

  • word.startswith("b") checks if the word starts with the letter “b”.
  • Only “banana” and “blueberry” match that rule.

Summary

List comprehension is just a shortcut to make lists. It’s clean and easy once you practice a few times.

Here's the basic pattern to remember:

[expression for item in collection if condition]

You can skip the if part if you don’t need to filter anything.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A list comprehension builds a new list by evaluating an expression for each selected item. The source iterable need not change.

Watch out for this idea

A list comprehension always modifies the source collection.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Iterators →