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)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay