Course Content
Python Data Structures
Python Data Structures
Dictionary 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[]
.
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)
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:
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)
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.
Thanks for your feedback!
Dictionary 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[]
.
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)
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:
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)
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.
Thanks for your feedback!
Dictionary 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[]
.
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)
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:
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)
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.
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[]
.
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)
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:
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)
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.