Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dictionary Comprehensions | Dictionary
Python Data Structures

## Dictionary Comprehensions

Dictionary comprehensions offer a succinct way to create dictionaries in Python. They are built in the same way as list comprehensions but with some exceptions.

## Basic Dictionary Comprehension

At its heart, a basic dictionary comprehension lets you construct a new dictionary by applying an expression to each key-value pair in an iterable variable.

Syntax:

What it does: For every `item` in `iterable`, it evaluates both `key_expression` and `value_expression` to create a new key-value pair in the dictionary.

Note

In contrast to lists, dictionaries require curly braces instead of square brackets. Additionally, in a dictionary comprehension, you specify both a key and a value separated by a colon, as in `key:value`, rather than just a single value.

Here, for each number `x` in the range `0` through `4`, we create a key-value pair where the key is the number and the value is its square.

It's equivalent to:

## Dictionary Comprehension with Condition

This variation allows you to introduce a condition to your dictionary comprehension, functioning as a filter. Only items that meet the condition are processed and added to the new dictionary.

Syntax: `{key_expression: value_expression for item in iterable if condition}`

What it does: For every `item` in `iterable`, if the condition is `True`, it evaluates both `key_expression` and `value_expression` and adds the resulting key-value pair to the dictionary.

In this instance, we only construct key-value pairs for numbers from the range `0` through `5` if they're even. The value represents the square of the key.

This example is equivalent to:

Dictionary comprehensions, like list comprehentions, are a more efficient and "Pythonic" way to craft dictionaries, often proving quicker in execution when compared to traditional loop methods.

Given a dictionary with cities and their respective populations, use dictionary comprehension to create a new dictionary that only contains cities with populations greater than a specified number.

Note

The expression `for city, population in cities_popul.items()` iterates over each key-value pair from the dictionary. During each loop, `city` holds the name of a city from the dictionary and `population` captures its associated population value.

Everything was clear?

Section 2. Chapter 9

Course Content

Python Data Structures

## Dictionary Comprehensions

Dictionary comprehensions offer a succinct way to create dictionaries in Python. They are built in the same way as list comprehensions but with some exceptions.

## Basic Dictionary Comprehension

At its heart, a basic dictionary comprehension lets you construct a new dictionary by applying an expression to each key-value pair in an iterable variable.

Syntax:

What it does: For every `item` in `iterable`, it evaluates both `key_expression` and `value_expression` to create a new key-value pair in the dictionary.

Note

In contrast to lists, dictionaries require curly braces instead of square brackets. Additionally, in a dictionary comprehension, you specify both a key and a value separated by a colon, as in `key:value`, rather than just a single value.

Here, for each number `x` in the range `0` through `4`, we create a key-value pair where the key is the number and the value is its square.

It's equivalent to:

## Dictionary Comprehension with Condition

This variation allows you to introduce a condition to your dictionary comprehension, functioning as a filter. Only items that meet the condition are processed and added to the new dictionary.

Syntax: `{key_expression: value_expression for item in iterable if condition}`

What it does: For every `item` in `iterable`, if the condition is `True`, it evaluates both `key_expression` and `value_expression` and adds the resulting key-value pair to the dictionary.

In this instance, we only construct key-value pairs for numbers from the range `0` through `5` if they're even. The value represents the square of the key.

This example is equivalent to:

Dictionary comprehensions, like list comprehentions, are a more efficient and "Pythonic" way to craft dictionaries, often proving quicker in execution when compared to traditional loop methods.

The expression `for city, population in cities_popul.items()` iterates over each key-value pair from the dictionary. During each loop, `city` holds the name of a city from the dictionary and `population` captures its associated population value.