Skip to main content

List Comprehension

When a new list is built from the values of an existing one, list comprehension lets you write it in far less code.

Say you have some fruits and want a new list holding only those whose name contains the letter "a".

The long way uses a for statement with a conditional test inside it:

Example

The long way, using a loop:

Prompt
Write a Python program that creates a list of fruits (apple, banana, cherry, kiwi, mango). Use a for loop with a conditional test to create a new list containing only fruits with the letter "a" in their name. Print the new list.
Output: complete Python program

Example

List comprehension does the same work in a single line:

Prompt
Write a Python program that creates a list of fruits (apple, banana, cherry, kiwi, mango). Use list comprehension to create a new list containing only fruits with the letter "a" in their name. Print the new list.
Output: complete Python program

The Syntax

You get back a new list, and the original stays as it was.

Syntax

main.py
newlist = [expression for item in iterable if condition == True]

Condition

Think of the condition as a filter: an item gets through only if the test evaluates to True.

Example

Keep every item except "apple":

Prompt
Write a Python program that uses list comprehension on a fruits list to create a new list that accepts all items except "apple". Print the new list.
Output: complete Python program

Example

Leaving out the if statement keeps every item:

Prompt
Write a Python program that uses list comprehension with no if statement to copy a fruits list into a new list. Print the new list.
Output: complete Python program

Iterable

The iterable may be any iterable object, such as a list, a tuple or a set.

Example

The range() function works as an iterable too:

Prompt
Write a Python program that uses list comprehension with the range() function to create a list of numbers from 0 to 9. Print the new list.
Output: complete Python program

Example

Same idea, keeping only numbers below 5:

Prompt
Write a Python program that uses list comprehension with range(10) to create a list that accepts only numbers lower than 5. Print the new list.
Output: complete Python program

Expression

The expression starts out as the current item of the iteration, and whatever it produces is what lands in the new list. That means you can transform the item on its way there.

Example

Convert each value to upper case for the new list:

Prompt
Write a Python program that uses list comprehension on a fruits list to set the values in the new list to upper case. Print the new list.
Output: complete Python program

Example

Make every value in the new list 'hello':

Prompt
Write a Python program that uses list comprehension on a fruits list to set all values in the new list to 'hello'. Print the new list.
Output: complete Python program

Example

Put "orange" wherever "banana" would appear:

Prompt
Write a Python program that uses list comprehension on a fruits list. Return "orange" instead of "banana", keeping all other items as they are. Print the new list.
Output: complete Python program