DEV Community

parrotypoisson
parrotypoisson

Posted on

Python Comprehensions

List comprehension

# [0, 1, 2, 3, 4]
listt = [x for x in range(5)]
Enter fullscreen mode Exit fullscreen mode

Dictionary comprehension

# {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
dictt = {x:x for x in range(5)}
Enter fullscreen mode Exit fullscreen mode

Set comprehension

# {0, 1, 2, 3, 4}
sett = {x for x in range(5)}
Enter fullscreen mode Exit fullscreen mode

Generator comprehension

# generator object
generator = (x for x in range(5))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)