Python Dictionary Comprehension

Summary: in this tutorial, you’ll learn about Python dictionary comprehension to transform or filter items in a dictionary.

Introduction to Python dictionary comprehension

A dictionary comprehension allows you to run a for loop on a dictionary and do something on each item like transforming or filtering and returns a new dictionary.

Unlike a for loop, a dictionary comprehension offers a more expressive and concise syntax when you use it correctly.

Here is the general syntax for dictionary comprehension:

{key:value for (key,value) in dict.items() if condition}Code language: CSS (css)

This dictionary comprehension expression returns a new dictionary whose item specified by the expression key: value

Python dictionary comprehension examples

We’ll take a look at how to use dictionary comprehension to transform and filter items in a dictionary.

1) Using Python dictionary comprehension to transform a dictionary

Suppose that you have the following dictionary whose items are stock symbol and price:

stocks = { 'AAPL': 121, 'AMZN': 3380, 'MSFT': 219, 'BIIB': 280, 'QDEL': 266, 'LVGO': 144 }Code language: Python (python)

To increase the price of each stock by 2%, you may come up with a for loop like this:

stocks = { 'AAPL': 121, 'AMZN': 3380, 'MSFT': 219, 'BIIB': 280, 'QDEL': 266, 'LVGO': 144 } new_stocks = {} for symbol, price in stocks.items(): new_stocks[symbol] = price*1.02 print(new_stocks) Code language: Python (python)

Output:

{'AAPL': 123.42, 'AMZN': 3447.6, 'MSFT': 223.38, 'BIIB': 285.6, 'QDEL': 271.32, 'LVGO': 146.88}Code language: Shell Session (shell)

How it works.

  • First, loop over the items of the stocks dictionary
  • Second, increase the price by 2% and add the item to the new dictionary (new_stocks).

The following example shows how to use dictionary comprehension to achieve the same result:

stocks = { 'AAPL': 121, 'AMZN': 3380, 'MSFT': 219, 'BIIB': 280, 'QDEL': 266, 'LVGO': 144 } new_stocks = {symbol: price * 1.02 for (symbol, price) in stocks.items()} print(new_stocks) Code language: Python (python)

This dictionary comprehension is equivalent to the for loop counterpart:

for loop

new_stocks = {} for symbol, price in stocks.items(): new_stocks[symbol] = price*1.02 Code language: Python (python)

dictionary comprehension

new_stocks = {symbol: price * 1.02 for (symbol, price) in stocks.items()} Code language: Python (python)

2) Using Python dictionary comprehension to filter a dictionary

To select stocks whose prices are greater than 200, you may use the following for loop:

stocks = { 'AAPL': 121, 'AMZN': 3380, 'MSFT': 219, 'BIIB': 280, 'QDEL': 266, 'LVGO': 144 } selected_stocks = {} for symbol, price in stocks.items(): if price > 200: selected_stocks[symbol] = price print(selected_stocks) Code language: Python (python)

How it works.

  • First, iterate over the item of the stocks dictionary
  • Then, add the item to the selected_stocks dictionary if the price is greater than 200

The following example uses the dictionary comprehension with an if clause to get the same result:

stocks = { 'AAPL': 121, 'AMZN': 3380, 'MSFT': 219, 'BIIB': 280, 'QDEL': 266, 'LVGO': 144 } selected_stocks = {s: p for (s, p) in stocks.items() if p > 200} print(selected_stocks)Code language: Python (python)

And you can compare the for loop and dictionary comprehension:

for loop

selected_stocks = {} for symbol, price in stocks.items(): if price > 200: selected_stocks[symbol] = price Code language: Python (python)

dictionary comprehension

selected_stocks = {s: p for (s, p) in stocks.items() if p > 200}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *