0Pricing
Python Academy · Lesson

Dictionary Comprehensions

Build dictionaries concisely with comprehension syntax.

Introduction

Dictionary comprehensions provide a concise way to build dicts from any iterable.

Basic Dict Comprehension

{k: v for k, v in items} is the base form. Example: {x: x**2 for x in range(5)} maps numbers to their squares.
squares = {x: x**2 for x in range(5)}
print(squares)

All lessons in this course

  1. Dictionary Basics
  2. Dictionary Methods
  3. Sets and Set Operations
  4. Dictionary Comprehensions
← Back to Python Academy