Editor

Double Every Number

Beginner · Write the code · from Python Comprehensions: List, Dict, Set, and Generator in One Guide

From the lesson: Python Comprehensions: List, Dict, Set, and Generator in One Guide

Imagine you have a list of 100 numbers and you want a new list containing only the even ones, each doubled. With a regular loop, that takes four lines. With a Python comprehension, it takes one.

Read the lesson

Use a list comprehension to create a list called doubled that contains each number from the given list, multiplied by 2.

Expected output:

[2, 4, 6, 8, 10]
numbers = [1, 2, 3, 4, 5]
# Create doubled using a list comprehension
print(doubled)

More Comprehensions exercises

All Python exercises · 50 practice problems · Data Structures quiz