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
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

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:

123456
squared_dict = {} for x in (0, 1, 2, 3, 4): squared_dict[x] = x * x print(squared_dict)
copy

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:

1234567
even_dict = {} for x in (0, 1, 2, 3, 4): if x % 2 == 0: even_dict[x] = x * x print(even_dict)
copy

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.

Task

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.

Task

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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 2. Chapter 9
toggle bottom row

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:

123456
squared_dict = {} for x in (0, 1, 2, 3, 4): squared_dict[x] = x * x print(squared_dict)
copy

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:

1234567
even_dict = {} for x in (0, 1, 2, 3, 4): if x % 2 == 0: even_dict[x] = x * x print(even_dict)
copy

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.

Task

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.

Task

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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 2. Chapter 9
toggle bottom row

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:

123456
squared_dict = {} for x in (0, 1, 2, 3, 4): squared_dict[x] = x * x print(squared_dict)
copy

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:

1234567
even_dict = {} for x in (0, 1, 2, 3, 4): if x % 2 == 0: even_dict[x] = x * x print(even_dict)
copy

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.

Task

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.

Task

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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

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:

123456
squared_dict = {} for x in (0, 1, 2, 3, 4): squared_dict[x] = x * x print(squared_dict)
copy

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:

1234567
even_dict = {} for x in (0, 1, 2, 3, 4): if x % 2 == 0: even_dict[x] = x * x print(even_dict)
copy

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.

Task

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.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 9
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt