← All advanced lessons

Advanced Python / A03

List Comprehension

Building concise lists from loops, expressions, and conditional logic.

Advanced Python - 3. List Comprehension

3. List Comprehension

3.1 Introduction

3.2 The "Long Way" (What We Already Know)

3.3 The "Short Way" (Introducing List Comprehension)

3.4 Adding a Condition (The if Statement)

Chapter 3 Assignments

A3.1 Perfect Squares

A3.2 Celsius Farenheit

A3.3 Perfect Squares

3. List Comprehension

3.1 Introduction

  1. Readability: Once the concept is understood, these single lines of code are often much easier to read and understand than a longer loop. They clearly state 'make a list by doing X for Y if Z'."
  2. Conciseness: Less lines of code means less chance for typos and faster coding.
  3. Performance: Python can often build these lists faster using list comprehensions than it can with a traditional for loop and append.
  4. It's a "Pythonic" Way: Experienced Python programmers use list comprehensions all the time. Learning this makes your code look more like professional Python code.

3.2 The "Long Way" (What We Already Know)

# Our original list of numbers

original_numbers = [1, 2, 3, 4, 5]

# An empty list to store our doubled numbers

doubled_numbers = []

# Now, we use a loop to go through each number and double it

for number in original_numbers:

new_number = number * 2

doubled_numbers.append(new_number)

print("Original Numbers:", original_numbers)

print("Doubled Numbers:", doubled_numbers)

(check the print statements. Make sure to change or explain how they work)

OUTPUT: Original Numbers: [1, 2, 3, 4, 5]

Doubled Numbers: [2, 4, 6, 8, 10]

3.3 The "Short Way" (Introducing List Comprehension)

  • Python has a way to do exactly the same thing, but in just one line. This is where list comprehension comes in.
  • List comprehension allows us to say:

Make me a new list where you do THIS action for every item IN this other list

# Our original list of numbers (same as before)

original_numbers = [1, 2, 3, 4, 5]

# *** List Comprehension! ***

doubled_numbers_comp = [number * 2 for number in original_numbers]

print("Doubled Numbers (the short way!):", doubled_numbers_comp)

OUTPUT: Doubled Numbers (the short way!): [2, 4, 6, 8, 10]

Explanation:

3.4 Adding a Condition (The if Statement)

The “long way”:

# Our original list of numbers (same as before)

original_numbers = [1, 2, 3, 4, 5]

# The long way with a condition

even_doubled_long = []

for number in original_numbers:

if number % 2 == 0: # Check if the number is even

even_doubled_long.append(number * 2)

print("\nEven Doubled Numbers (the long way):", even_doubled_long)

OUTPUT: Even Doubled Numbers (the long way): [4, 8]

With List Comprehension, we can just add that if statement right into our one line!:

# Our original list of numbers (same as before)

original_numbers = [1, 2, 3, 4, 5]

# *** List Comprehension! ***

even_doubled_comp = [number * 2 for number in original_numbers if number % 2 == 0]

print("Even Doubled Numbers (the short way!):", even_doubled_comp)

OUTPUT: Even Doubled Numbers (the short way!): [4, 8]

Explanation:

Chapter 3 Assignments

A3.1 Perfect Squares

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

A3.2 Celsius Farenheit

Fahrenheit = Celsius * 1.8 + 32

A3.3 Perfect Squares

  • From the list of words: ['apple', 'banana', 'kiwi', 'grape', 'orange'], create a new list containing only the words that have 5 letters or more. This will need a conditional.