Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python comprehensions code

Practice writing list, set, and dictionary comprehensions and using filters.

Intermediate ~8 min read

Challenge 1

Create a list comprehension containing the squares of the numbers.

python Your answer
numbers = range(1, 5)
squares = 

print(squares)

Press Ctrl⌘Enter to check your answer

Challenge 2

Create a list containing only the even numbers.

python Your answer
numbers = range(1, 8)
evens = 

print(evens)

Press Ctrl⌘Enter to check your answer

Challenge 3

Create a set containing the unique letters from the word.

python Your answer
word = "banana"
letters = 

print(letters)

Press Ctrl⌘Enter to check your answer

Challenge 4

Create a dictionary that maps each number to its square.

python Your answer
numbers = range(1, 5)
squares = 

print(squares)

Press Ctrl⌘Enter to check your answer