Loading…
List comprehensions create lists in one line.
squares = []
for x in range(5):
squares.append(x ** 2)
squares = [x ** 2 for x in range(5)]
[0, 1, 4, 9, 16]
[expression for item in iterable]
Create a list of the first 10 positive integers doubled (2, 4, 6, ..., 20) using a list comprehension. Print the result.
Downloading Python… (one-time, ~10 MB)
This runs entirely in your browser and is cached for next time.