OSPython.020: List Comprehensions Basics

OSPython.020 List Comprehensions Basics cover with an accurately color-coded VS Code Dark+ Python editor comparing a normal for loop with list comprehensions

A list comprehension is a short way to build a new Python list from another iterable.

The easiest way to understand it is to start with a normal for loop and then shorten that same idea.

Start with a normal loop

numbers = [1, 2, 3, 4]
squares = []

for number in numbers:
    squares.append(number * number)

print(squares)

Output:

[1, 4, 9, 16]

This code says: take each number, square it, and add the result to a new list.

Now write the same idea as a list comprehension

numbers = [1, 2, 3, 4]
squares = [number * number for number in numbers]

print(squares)

The output is still:

[1, 4, 9, 16]

Nothing magical happened. Python simply gives us a compact form for this common pattern.

Read it in three pieces

[number * number  for number  in numbers]
 └──── result ───┘ └─ loop ─┘ └ source ┘
  • number * number — the value to put in the new list.
  • for number — take one item at a time.
  • in numbers — get those items from numbers.

If the one-line form feels confusing, write the normal loop first. Once the loop makes sense, the comprehension is much easier to read.

Video 1: List comprehensions in a few minutes

This beginner lesson demonstrates the basic Python list-comprehension pattern and compares it with ordinary loops.

You can also copy items into a new list

players = ["Ava", "Jay", "Mia"]

new_players = [player for player in players]

print(new_players)

Output:

['Ava', 'Jay', 'Mia']

This example does not change each item. It simply shows the structure in its easiest form.

Transform each item

A comprehension becomes more useful when the new list needs a changed version of each item.

prices = [10, 20, 30]

doubled = [price * 2 for price in prices]

print(doubled)

Output:

[20, 40, 60]

The source list stays [10, 20, 30]. The comprehension creates a new list containing the calculated values.

Video 2: Making list comprehensions easier to read

This tutorial breaks the syntax into a simple pattern so the one-line form is easier to remember.

Add one simple filter

You can add an if condition when only some items should enter the new list.

numbers = [1, 2, 3, 4, 5, 6]

evens = [number for number in numbers if number % 2 == 0]

print(evens)

Output:

[2, 4, 6]

Read it like this: put the number in the new list for each number in numbers if that number is even.

Gaming example

Suppose a game has player scores and we only want scores of 100 or higher.

scores = [55, 120, 88, 150, 40]

high_scores = [score for score in scores if score >= 100]

print(high_scores)

Output:

[120, 150]

The comprehension creates a new list containing only the scores that pass the condition.

Video 3: Building lists with comprehensions

This lesson reinforces how a list comprehension constructs a new list from a compact loop expression.

How this connects to earlier Python lessons

OSPython.002: Lists, Tuples, and Sets introduced lists. OSPython.004: For Loops, Range, and Iteration introduced the loop used inside a comprehension. OSPython.019: Generators and yield Basics introduced another way Python can produce values through iteration.

When should you use the normal loop instead?

A list comprehension is useful when the operation is short and easy to understand. If the logic needs many steps, several conditions, logging, error handling, or other side effects, a normal loop is often clearer.

Shorter code is not automatically better code. The goal is code that another person can understand.

Common beginner mistakes

  • Forgetting the square brackets [ ].
  • Putting the pieces in the wrong order.
  • Trying to squeeze complicated multi-step logic into one line.
  • Assuming a comprehension changes the original list when it actually creates a new list in these examples.
  • Adding an if filter before understanding the basic no-filter form.

Quick practice

  1. Start with numbers = [1, 2, 3, 4].
  2. Use a normal loop to create a list containing each number multiplied by 10.
  3. Rewrite that loop as a list comprehension.
  4. Create another comprehension that keeps only numbers greater than 2.
  5. Explain which part is the result expression, which part is the loop, and which part is the source.

Key takeaway

A list comprehension is a compact way to build a new list. Learn it by comparing it with the normal for loop first. Keep the one-line form when it stays easy to read; use a normal loop when the job becomes complicated.

One response to “OSPython.020: List Comprehensions Basics”

  1. […] OSPython.020: List Comprehensions Basics showed another compact Python syntax. In both cases, compact code is useful only when it stays easy to read. […]

    Like

Leave a comment