SyntaxStudy
Sign Up
Python Dictionary Comprehensions
Python Beginner 3 min read

Dictionary Comprehensions

Dictionary Comprehensions

Dict comprehensions use curly braces with a key:value expression to build dictionaries concisely.

Example
words = ["apple", "banana", "cherry"]
lengths = {word: len(word) for word in words}
print(lengths)
# {"apple": 5, "banana": 6, "cherry": 6}
Pro Tip

Dict comprehensions are great for transforming or inverting mappings.