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

bookDictionary Comprehensions

Dictionary comprehensions provide a concise and elegant way to create dictionaries in Python. They follow a similar structure to list comprehensions but with some key differences.

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:

  • key_expression: defines the key for each key-value pair;
  • value_expression: defines the value corresponding to the key;
  • iterable: the source of items to be processed (e.g., a list, range, or another iterable).

Note

Unlike lists, dictionaries require both a key and a value, separated by a colon (:) and use {} instead of [].

1234567891011
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Create a dictionary using dictionary comprehension book_dict = {title: year for title, year in books} print(book_dict)
copy

For each tuple in the books list, the title is used as the key, and the year is used as the value.

The same dictionary can be created using a for loop:

123456789101112
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] book_dict = {} for title, year in books: book_dict[title] = year print(book_dict)
copy
Task
test

Swipe to show code editor

A bookstore wants to create a dictionary that maps book titles to their prices. Use dictionary comprehension to create a new dictionary called book_prices from the following list of lists:

Transform the books list into a dictionary using dictionary comprehension, where the title is the key, and the price is the value.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 10
toggle bottom row

bookDictionary Comprehensions

Dictionary comprehensions provide a concise and elegant way to create dictionaries in Python. They follow a similar structure to list comprehensions but with some key differences.

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:

  • key_expression: defines the key for each key-value pair;
  • value_expression: defines the value corresponding to the key;
  • iterable: the source of items to be processed (e.g., a list, range, or another iterable).

Note

Unlike lists, dictionaries require both a key and a value, separated by a colon (:) and use {} instead of [].

1234567891011
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Create a dictionary using dictionary comprehension book_dict = {title: year for title, year in books} print(book_dict)
copy

For each tuple in the books list, the title is used as the key, and the year is used as the value.

The same dictionary can be created using a for loop:

123456789101112
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] book_dict = {} for title, year in books: book_dict[title] = year print(book_dict)
copy
Task
test

Swipe to show code editor

A bookstore wants to create a dictionary that maps book titles to their prices. Use dictionary comprehension to create a new dictionary called book_prices from the following list of lists:

Transform the books list into a dictionary using dictionary comprehension, where the title is the key, and the price is the value.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 10
toggle bottom row

bookDictionary Comprehensions

Dictionary comprehensions provide a concise and elegant way to create dictionaries in Python. They follow a similar structure to list comprehensions but with some key differences.

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:

  • key_expression: defines the key for each key-value pair;
  • value_expression: defines the value corresponding to the key;
  • iterable: the source of items to be processed (e.g., a list, range, or another iterable).

Note

Unlike lists, dictionaries require both a key and a value, separated by a colon (:) and use {} instead of [].

1234567891011
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Create a dictionary using dictionary comprehension book_dict = {title: year for title, year in books} print(book_dict)
copy

For each tuple in the books list, the title is used as the key, and the year is used as the value.

The same dictionary can be created using a for loop:

123456789101112
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] book_dict = {} for title, year in books: book_dict[title] = year print(book_dict)
copy
Task
test

Swipe to show code editor

A bookstore wants to create a dictionary that maps book titles to their prices. Use dictionary comprehension to create a new dictionary called book_prices from the following list of lists:

Transform the books list into a dictionary using dictionary comprehension, where the title is the key, and the price is the value.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Dictionary comprehensions provide a concise and elegant way to create dictionaries in Python. They follow a similar structure to list comprehensions but with some key differences.

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:

  • key_expression: defines the key for each key-value pair;
  • value_expression: defines the value corresponding to the key;
  • iterable: the source of items to be processed (e.g., a list, range, or another iterable).

Note

Unlike lists, dictionaries require both a key and a value, separated by a colon (:) and use {} instead of [].

1234567891011
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Create a dictionary using dictionary comprehension book_dict = {title: year for title, year in books} print(book_dict)
copy

For each tuple in the books list, the title is used as the key, and the year is used as the value.

The same dictionary can be created using a for loop:

123456789101112
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] book_dict = {} for title, year in books: book_dict[title] = year print(book_dict)
copy
Task
test

Swipe to show code editor

A bookstore wants to create a dictionary that maps book titles to their prices. Use dictionary comprehension to create a new dictionary called book_prices from the following list of lists:

Transform the books list into a dictionary using dictionary comprehension, where the title is the key, and the price is the value.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 10
Switch to desktopSwitch 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