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
.
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.
If you want to set them all to 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.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.
giving
you can't hash a list like that. try this instead, it uses tuples
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
or
you should write only
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:
You're receiving a SyntaxError because when you write
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
and not
but lists are not hashable, so you can't use them as dict keys.
You can use the
dict.fromkeys
class method ...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:
If you don't actually need to initialize all the keys, a
defaultdict
might be useful as well:To answer the second part, a dict-comprehension is just what you need:
You probably shouldn't do this but you could also create a subclass of
dict
which works somewhat like adefaultdict
if you override__missing__
: