54
votes

I'm wondering if there's any way to populate a dictionary such that you have multiple keys mapping to the same value that's less verbose than say:

d = {1:'yes', 2:'yes', 3:'yes', 4:'no'}

I'm thinking something along the lines of:

d = {*(1,2,3):'yes', 4:'no'}

which is obviously a syntax error.

Is there a reasonably simple method of doing this without TOO much obfuscation? (I'm not playing code golf, but I also don't need to write essentially the same thing over and over. However, any code-golf related answers would be appreciated as well since code-golf is awesome =]).

Edit:

I probably picked a bad example. This is what I'm trying to do:

d = {*('READY', 95): 'GPLR2_95', 'CHARGING': 'GPLR3_99', 'PROTECTION': 'GPLR3_100', 'CONNECTED': 'GPLR3_101', 'ERROR':'GPLR3_102'}

What I would expect this to expand to is:

d = {'READY':'GPLR2_95', 95: 'GPLR2_95', ...}

Edit->Edit:

I know this is stupid and totally unnecessary, but my goal is to make this declaration on a single line. This obviously shouldn't limit any responses and writing code just because it fits on 1 line is stupid. But I'm writing a module level constant dict that would be nice if it was a single liner.

7
Obviously, d = collections.defaultdict(lambda : 'yes') is not what you are looking for? - khachik
Reversing your keys would work as well... 'yes': (1,2,3) - Nix
@Nix: Yes it's just my example. I'll update it to be slightly more in line with my application. - Falmarri
No need, just wanted to make sure a different way of thinking about it didn't remove your need to do it.. - Nix
Oh, for crying out loud. Write a little function that takes the tuple and vector as arguments. Write an iterator. Write it, for Gods' sakes, on more than one line. Someone else may have to read this someday. - Charlie Martin

7 Answers

82
votes

You could turn it around:

>>> d1 = {"yes": [1,2,3], "no": [4]}

and then "invert" that dictionary:

>>> d2 = {value:key for key in d1 for value in d1[key]}
>>> d2
{1: 'yes', 2: 'yes', 3: 'yes', 4: 'no'}
9
votes

How about:

501 $ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {"q":1}
>>> print a
{'q': 1}
>>> a["q"]
1
>>> a["r"] = a["s"] = a["t"] = 2
>>> a
{'q': 1, 's': 2, 'r': 2, 't': 2}
>>> 
4
votes

Code golf?

yesindices = [1,2,3,22,34,33]
noindices = [4,8,9]
dict (zip(yesindices, ['yes' for i in yesindices]) + zip(noindices, ['no' for i in noindices]))

yields

{1: 'yes', 2: 'yes', 3: 'yes', 4: 'no', 33: 'yes', 8: 'no', 9: 'no', 34: 'yes', 22: 'yes'}
2
votes
d = {'READY': 'GPLR2_95',
    95: 'GPLR2_95',
    'CHARGING': 'GPLR3_99',
    'PROTECTION': 'GPLR3_100',
    'CONNECTED': 'GPLR3_101',
    'ERROR':'GPLR3_102'}

What's wrong with breaking this into multiple lines (as above)? Is the point saving typing or saving vertical space? Something else?

BTW, it feels really strange to have keys that are a mix of numbers and strings.

note: I wrote this as an answer instead of a comment because I wanted to show formatted code on multiple lines.

1
votes
dict((x, {4: 'no'}.get(x, 'yes')) for x in range(1, 5))

Or in 3.x:

{x: {4: 'no'}.get(x, 'yes') for x in range(1, 5)}
1
votes

For your case

dict([(_, 'yes') for _ in range(1,4)], **{4:'no'})

And if you need multiple keys for 'yes' and 'no'

>>> from itertools import chain
>>> dict(chain([(_, 'yes') for _ in range(1,4)], [(_, 'no') for _ in range(4, 10)]))
{1: 'yes', 2: 'yes', 3: 'yes', 4: 'no', 5: 'no', 6: 'no', 7: 'no', 8: 'no', 9: 'no'}

Not so great, but works.

1
votes

Try to iterate through the dict items.

  • For python 2.x

{key: value for key, value in your_dict.iteritems()}

  • For python 3.x

{key: value for key, value in your_dict.items()}