Python Dictionary Comprehension (original) (raw)

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

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

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)

Try it

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.

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)

Try it

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)

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)

Try it

How it works.

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)

Try it

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} Code language: Python (python)

Summary #

Quiz #

Was this tutorial helpful ?