search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
near_me
Linear Algebra
54 guides
keyboard_arrow_down
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

# Python | Dictionary Comprehension

schedule Aug 12, 2023
Last updated
local_offer
Python
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Dictionary comprehension in Python allows us to create a new dictionary in a short, efficient manner.

# Syntax

## Basic

``` {key expression: value expression for variable in iterable} ```

## Conditional

``` {output expression + conditional on output for variable in iterable + conditional on iterable} ```

# Examples

## Basic

To create a dictionary with keys 1 ~ 3, and values as `2*key`:

``` doubles = {i: 2*i for i in range(1, 4)}print(doubles) {1: 2, 2: 4, 3: 6} ```

For every element `i` in the `range(1,4)`, we are adding a key-value pair `i: 2*i` to the `doubles `dictionary.

To create a new dictionary `d_new` by doubling the values of an existing dictionary `d_old`:

``` d_old = {'a': 1, 'b': 2, 'c': 3}d_new = {key:value*2 for (key,value) in d_old.items()}print(d_new) {'a': 2, 'b': 4, 'c': 6} ```

## Conditional

### Input condition

To specify a condition on the input:

``` d_old = {'a': 1, 'b': 2, 'c': 3}d_new = {key:value*2 for (key,value) in d_old.items() if key != 'c'}print(d_new) {'a': 2, 'b': 4} ```

Here, we only loop through `(key,value)` in the `for` loop where `key` does not equal `'c'`. Hence, we do not see any key-value pair in the output with key `'c'`.

To specify multiple conditions on the input:

``` d_old = {'a': 1, 'b': 2, 'c': 3}d_new = {key:value*2 for (key,value) in d_old.items() if key != 'c' if value % 2 == 0} {'b': 4} ```

Here we specify two conditions for the input iterable `d_old`:

1) `key` must not equal `'c'`

2) `value` must be divisible by 2

### Output condition

To specify a condition on the output:

``` d_old = {'a': 1, 'b': 2, 'c': 3}d_new = {key:(value*2 if key!='c' else value*3) for (key,value) in d_old.items()}print(d_new) {'a': 2, 'b': 4, 'c': 9} ```

Here, we add `key:value*2` to `d_new` if the key does not equal `'c'`, otherwise we add `key:value*3`. Notice how only the value of key `'c'` has been multiplied by 3 while the value of keys `'a'` and `'b'` have only been multiplied by 2.

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!