Set Comprehension in Python
Level 3103 keystrokes
This creates a set using comprehension. Set comprehensions automatically remove duplicates and use curly braces.
Python code
# A list with many duplicate values1numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]# Set comprehension: squares each number; the set keeps each result only once# The result is {1, 4, 9, 16}2unique_squares = {x**2 for x in numbers}# Sets are unordered, so the display order may differ: {16, 1, 4, 9}3print(unique_squares)Output
{16, 1, 4, 9}Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
,× 9_× 2*× 2=× 2(× 1)× 1[× 1]× 1{× 1}× 1