Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dictionary Comprehension with Condition | Словник
Структури Даних в Python
course content

Зміст курсу

Структури Даних в Python

Структури Даних в Python

1. Список
2. Словник
3. Кортеж
4. Множина

bookDictionary Comprehension with Condition

You can include a condition in a dictionary comprehension to filter items. For example, let's create a dictionary of books published before 1950:

1234567891011
books = [ ["Pride and Prejudice", 1813], ["1984", 1949], ["To Kill a Mockingbird", 1960], ["The Great Gatsby", 1925] ] # Filter books published before 1950 books_dictionary = {title: year for title, year in books if year < 1950} print(books_dictionary)
copy

Adding a condition allows you to filter data dynamically while creating the dictionary.

Here's the equivalent code using a for loop instead of a dictionary comprehension:

123456789101112131415
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Filter books published before 1950 using a for loop filtered_books = {} for title, year in books: if year < 1950: filtered_books[title] = year print(filtered_books)
copy
Завдання
test

Swipe to show code editor

The bookstore wants to create a dictionary of books that cost less than $12. Use dictionary comprehension with a condition to create a new dictionary called discount_books from the following list of lists:

  1. Iterate through the books list by unpacking its elements (lists) into title and price.
  2. Apply the filter if price < 12 in the comprehension to include only books with a price below $12.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 11
toggle bottom row

bookDictionary Comprehension with Condition

You can include a condition in a dictionary comprehension to filter items. For example, let's create a dictionary of books published before 1950:

1234567891011
books = [ ["Pride and Prejudice", 1813], ["1984", 1949], ["To Kill a Mockingbird", 1960], ["The Great Gatsby", 1925] ] # Filter books published before 1950 books_dictionary = {title: year for title, year in books if year < 1950} print(books_dictionary)
copy

Adding a condition allows you to filter data dynamically while creating the dictionary.

Here's the equivalent code using a for loop instead of a dictionary comprehension:

123456789101112131415
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Filter books published before 1950 using a for loop filtered_books = {} for title, year in books: if year < 1950: filtered_books[title] = year print(filtered_books)
copy
Завдання
test

Swipe to show code editor

The bookstore wants to create a dictionary of books that cost less than $12. Use dictionary comprehension with a condition to create a new dictionary called discount_books from the following list of lists:

  1. Iterate through the books list by unpacking its elements (lists) into title and price.
  2. Apply the filter if price < 12 in the comprehension to include only books with a price below $12.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 11
toggle bottom row

bookDictionary Comprehension with Condition

You can include a condition in a dictionary comprehension to filter items. For example, let's create a dictionary of books published before 1950:

1234567891011
books = [ ["Pride and Prejudice", 1813], ["1984", 1949], ["To Kill a Mockingbird", 1960], ["The Great Gatsby", 1925] ] # Filter books published before 1950 books_dictionary = {title: year for title, year in books if year < 1950} print(books_dictionary)
copy

Adding a condition allows you to filter data dynamically while creating the dictionary.

Here's the equivalent code using a for loop instead of a dictionary comprehension:

123456789101112131415
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Filter books published before 1950 using a for loop filtered_books = {} for title, year in books: if year < 1950: filtered_books[title] = year print(filtered_books)
copy
Завдання
test

Swipe to show code editor

The bookstore wants to create a dictionary of books that cost less than $12. Use dictionary comprehension with a condition to create a new dictionary called discount_books from the following list of lists:

  1. Iterate through the books list by unpacking its elements (lists) into title and price.
  2. Apply the filter if price < 12 in the comprehension to include only books with a price below $12.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

You can include a condition in a dictionary comprehension to filter items. For example, let's create a dictionary of books published before 1950:

1234567891011
books = [ ["Pride and Prejudice", 1813], ["1984", 1949], ["To Kill a Mockingbird", 1960], ["The Great Gatsby", 1925] ] # Filter books published before 1950 books_dictionary = {title: year for title, year in books if year < 1950} print(books_dictionary)
copy

Adding a condition allows you to filter data dynamically while creating the dictionary.

Here's the equivalent code using a for loop instead of a dictionary comprehension:

123456789101112131415
books = [ ("Pride and Prejudice", 1813), ("1984", 1949), ("To Kill a Mockingbird", 1960), ("The Great Gatsby", 1925) ] # Filter books published before 1950 using a for loop filtered_books = {} for title, year in books: if year < 1950: filtered_books[title] = year print(filtered_books)
copy
Завдання
test

Swipe to show code editor

The bookstore wants to create a dictionary of books that cost less than $12. Use dictionary comprehension with a condition to create a new dictionary called discount_books from the following list of lists:

  1. Iterate through the books list by unpacking its elements (lists) into title and price.
  2. Apply the filter if price < 12 in the comprehension to include only books with a price below $12.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 2. Розділ 11
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt