By Rushy Panchal


2013-01-24 17:51:45 8 Comments

Is it possible to create a dictionary comprehension in Python (for the keys)?

Without list comprehensions, you can use something like this:

l = []
for n in range(1, 11):
    l.append(n)

We can shorten this to a list comprehension: l = [n for n in range(1, 11)].

However, say I want to set a dictionary's keys to the same value. I can do:

d = {}
for n in range(1, 11):
     d[n] = True # same value for each

I've tried this:

d = {}
d[i for i in range(1, 11)] = True

However, I get a SyntaxError on the for.

In addition (I don't need this part, but just wondering), can you set a dictionary's keys to a bunch of different values, like this:

d = {}
for n in range(1, 11):
    d[n] = n

Is this possible with a dictionary comprehension?

d = {}
d[i for i in range(1, 11)] = [x for x in range(1, 11)]

This also raises a SyntaxError on the for.

8 comments

@mgilson 2013-01-24 17:53:50

You can use the dict.fromkeys class method ...

>>> dict.fromkeys(range(5), True)
{0: True, 1: True, 2: True, 3: True, 4: True}

This is the fastest way to create a dictionary where all the keys map to the same value.

But do not use this with mutable objects:

d = dict.fromkeys(range(5), [])
# {0: [], 1: [], 2: [], 3: [], 4: []}
d[1].append(2)
# {0: [2], 1: [2], 2: [2], 3: [2], 4: [2]} !!!

If you don't actually need to initialize all the keys, a defaultdict might be useful as well:

from collections import defaultdict
d = defaultdict(True)

To answer the second part, a dict-comprehension is just what you need:

{k: k for k in range(10)}

You probably shouldn't do this but you could also create a subclass of dict which works somewhat like a defaultdict if you override __missing__:

>>> class KeyDict(dict):
...    def __missing__(self, key):
...       #self[key] = key  # Maybe add this also?
...       return key
... 
>>> d = KeyDict()
>>> d[1]
1
>>> d[2]
2
>>> d[3]
3
>>> print(d)
{}

@rhbvkleef 2018-01-22 11:42:29

Note that in the case of d = defaultdict(lambda: True), the lambda is not required as True is (or shouldn't) be mutable.

@BrenBarn 2013-01-24 17:54:24

There are dictionary comprehensions in Python 2.7+, but they don't work quite the way you're trying. Like a list comprehension, they create a new dictionary; you can't use them to add keys to an existing dictionary. Also, you have to specify the keys and values, although of course you can specify a dummy value if you like.

>>> d = {n: n**2 for n in range(5)}
>>> print d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

If you want to set them all to True:

>>> d = {n: True for n in range(5)}
>>> print d
{0: True, 1: True, 2: True, 3: True, 4: True}

What you seem to be asking for is a way to set multiple keys at once on an existing dictionary. There's no direct shortcut for that. You can either loop like you already showed, or you could use a dictionary comprehension to create a new dict with the new values, and then do oldDict.update(newDict) to merge the new values into the old dict.

@mgilson 2013-01-24 18:19:12

FWIW, dict.update can also accept an iterable of key-value pairs just like the dict constructor

@Martijn Pieters 2016-05-14 13:27:48

Note that if you to create a dictionary with all values the same, use dict.fromkeys(). So to set all values to True, use dict.fromkeys(range(5), True). Watch out, the value is not copied, so you may want to avoid this when you have a mutable value; it'll be shared between all keys.

@Zaaier 2017-04-24 15:27:37

Note: the keys can be the result of a method as well: { n*2 : n for n in range(3) } => {0: 0, 2: 1, 4: 2}. Both can be done in the same expression: { n*2 : n*3 for n in range(3) } => { 0: 0, 2: 3, 4: 6 }.

@adonese 2016-02-17 19:51:40

Consider this example of counting the occurrence of words in a list using dictionary comprehension

my_list = ['hello', 'hi', 'hello', 'today', 'morning', 'again', 'hello']
my_dict = {k:my_list.count(k) for k in my_list}
print(my_dict)

And the result is

{'again': 1, 'hi': 1, 'hello': 3, 'today': 1, 'morning': 1}

@FuriousGeorge 2017-03-08 15:17:19

This is interesting, though not the most efficient as you'll be counting keys like 'hello' multiple times

@ehontz 2014-08-16 18:03:34

I really like the @mgilson comment, since if you have a two iterables, one that corresponds to the keys and the other the values, you can also do the following.

keys = ['a', 'b', 'c']
values = [1, 2, 3]
d = dict(zip(keys, values))

giving

d = {'a': 1, 'b': 2, 'c': 3}

@Bonifacio2 2013-01-24 18:53:48

The main purpose of a list comprehension is to create a new list based on another one without changing or destroying the original list.

Instead of writing

    l = []
    for n in range(1, 11):
        l.append(n)

or

    l = [n for n in range(1, 11)]

you should write only

    l = range(1, 11)

In the two top code blocks you're creating a new list, iterating through it and just returning each element. It's just an expensive way of creating a list copy.

To get a new dictionary with all keys set to the same value based on another dict, do this:

    old_dict = {'a': 1, 'c': 3, 'b': 2}
    new_dict = { key:'your value here' for key in old_dict.keys()}

You're receiving a SyntaxError because when you write

    d = {}
    d[i for i in range(1, 11)] = True

you're basically saying: "Set my key 'i for i in range(1, 11)' to True" and "i for i in range(1, 11)" is not a valid key, it's just a syntax error. If dicts supported lists as keys, you would do something like

    d[[i for i in range(1, 11)]] = True

and not

    d[i for i in range(1, 11)] = True

but lists are not hashable, so you can't use them as dict keys.

@Aditya Sihag 2013-01-24 17:58:12

you can't hash a list like that. try this instead, it uses tuples

d[tuple([i for i in range(1,11)])] = True

@Ned Batchelder 2013-01-24 18:31:23

This doesn't do at all what the OP wanted...

@NPE 2013-01-24 17:54:49

>>> {i:i for i in range(1, 11)}
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}

@Bryan 2013-01-24 17:55:26

Use dict() on a list of tuples, this solution will allow you to have arbitrary values in each list, so long as they are the same length

i_s = range(1, 11)
x_s = range(1, 11)
# x_s = range(11, 1, -1) # Also works
d = dict([(i_s[index], x_s[index], ) for index in range(len(i_s))])

@mgilson 2013-01-24 18:12:03

As a side note, this is the same thing as d = dict(zip(i_s,x_s))

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 2845175 View
  • 2683 Score
  • 23 Answer
  • Tags:   python dictionary

21 Answered Questions

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

12 Answered Questions

10 Answered Questions

[SOLVED] How to return dictionary keys as a list in Python?

14 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

15 Answered Questions

[SOLVED] What are metaclasses in Python?

15 Answered Questions

[SOLVED] Add new keys to a dictionary?

39 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

56 Answered Questions

[SOLVED] Calling an external command in Python

Sponsored Content