AI engineering glossary · Python core

What is dictionary comprehension?

Dictionary comprehension: A compact expression that builds a dictionary from an iterable using key: value pairs, written {k: v for item in iterable if cond}. It is used to transform, filter or invert dictionaries.

Level: intermediateTopic: Python core

Dictionary comprehension: an example

{name: len(name) for name in ['Ravi', 'Sita']} gives {'Ravi': 4, 'Sita': 4}.

Related terms

More Python core terms

Where you learn it

Dictionary comprehension is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.