8

What is the cleanest way to update the values of multiple keys in a dictionary to the values stored in a tuple?

Example:

I want to go from

>>>mydict = {'a':None, 'b':None, 'c':None, 'd':None}
>>>mytuple = ('alpha', 'beta', 'delta')

to

>>>print mydict
{'a':'alpha', 'b':'beta', 'c':None, 'd':'delta'}

Is there an easy one-liner for this? Something like this seems to be getting close to what I want.

EDIT: I don't wish to assign values to keys based on their first letter. I'm just hoping for something like

mydict('a','b','d') = mytuple

Obviously that doesn't work, but I'm hoping for something similar to that.

Community
  • 1
  • 1
nfazzio
  • 498
  • 1
  • 3
  • 12
  • 4
    What rule are you using in your head to associate the value `alpha` with the key `a`? That the value starts with `a`? – Eric Oct 11 '13 at 23:16
  • Check this link: [http://stackoverflow.com/questions/16129405/multiple-keys-in-python-dictionary-is-possible][1] [1]: http://stackoverflow.com/questions/16129405/multiple-keys-in-python-dictionary-is-possible – ipinak Oct 11 '13 at 23:16
  • @ipinak: I don't see the relevance – Eric Oct 11 '13 at 23:23
  • Eric, apologies for not being clear in my original question. I've updated it to reflect what I want. – nfazzio Oct 11 '13 at 23:29
  • @eric the first answer of that post gives the answer to the requested problem. It's indirect somehow, but still the answer is there as well. I didn't say it's a duplicate, I just forward to that post. So, I believe it is relevant. – ipinak Oct 11 '13 at 23:49

4 Answers4

7

If you're trying to create a new dictionary:

d = dict(zip(keys, valuetuple))

If you're trying to add to an existing one, just change the = to .update(…).

So, your example can be written as:

mydict.update(dict(zip('abd', mytuple))))

If you're doing this more than once, I'd wrap it up in a function, so you can write:

setitems(d, ('a', 'b', 'd'), mytuple)

Or maybe a "curried" function that parallels operator.itemgetter?

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • This works! mydict.update(dict(zip(['a','b','d'],mytuple))) worked wonders! – nfazzio Oct 11 '13 at 23:38
  • @nfazzio: I just realized I used 'abd' as shorthand for your ['a', 'b', 'd']. That will work (since a string is a sequence of 1-character strings), but I think your way is clearer. – abarnert Oct 11 '13 at 23:46
0

You could use list comprehension to make it a one-liner, though it's not super efficient.

keys = [ 'a', 'b', 'c', 'd']
values = ['alpha', 'beta', 'delta']
dictionary = dict([(k,v) for k in keys for v in values if v.startswith(k)])
print dictionary #prints {'a': 'alpha', 'b': 'beta', 'd': 'delta'}
Justin Jasmann
  • 2,363
  • 2
  • 14
  • 18
  • Justin, this isn't quite what I want. I want to update existing keys in a dictionary with values from a tuple. Thanks for the extra startswith method, but it's unnecessary - I've updated my question to reflect that it isn't necessary. – nfazzio Oct 11 '13 at 23:32
0

Assuming the association key <-> value is the first letter of the value is the first letter of the key.

dict( (v[0],v) for v in mytuple if v[0] in mydict)
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
  • This kind of thing can be written slightly more neatly with a dictionary comprehension: `{v[0]: v for v in mytuple if v[0] in mydict}` – Matthias Fripp Jan 06 '16 at 03:41
  • This misses the `c` key. The result would be `{'a': 'alpha', 'b': 'beta', 'd': 'delta'}` – aydow Aug 16 '17 at 03:22
0

I would avoid one-liners in this case. It makes code more readable.

for t in mytuple:
    if t[0] in mydict.keys():
        mydict[t[0]] = t

If you want to add mytuple items even if the key does not exist, simply remove the if statement.

micromoses
  • 6,747
  • 2
  • 20
  • 29