Dictionary Comprehensions
Dict comprehensions use curly braces with a key:value expression to build dictionaries concisely.
Dict comprehensions use curly braces with a key:value expression to build dictionaries concisely.
words = ["apple", "banana", "cherry"]
lengths = {word: len(word) for word in words}
print(lengths)
# {"apple": 5, "banana": 6, "cherry": 6}
Dict comprehensions are great for transforming or inverting mappings.