1

Say, I have a dictionary of data and their variable names, e.g.

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

and I want to retrieve the values associated with a subset of keys. How can I do this without coding too much, i.e. extracting the variables one-by-one? Is there something like

a, c = dct['a','c']
kirelagin
  • 13,248
  • 2
  • 42
  • 57
Jan
  • 4,932
  • 1
  • 26
  • 30
  • This question is a duplicate: http://stackoverflow.com/questions/3803419/create-variables-from-strings-in-python – Brent Washburne Jun 07 '13 at 21:45
  • I don't think it's a dup. He doesn't want to make a local variable for every key in the dict, he just wants to extract more than one key at a time. – abarnert Jun 07 '13 at 21:49
  • Yes, that is what I am looking for. Extracting a subset of values without doing this one-by-one. I have edited by question accordingly – Jan Jun 07 '13 at 21:51
  • @abarnert It seems like he does want to make local variables. Otherwise this question seems to be in a liminal state of practicality – jamylak Jun 07 '13 at 21:51
  • OK. What I have understood is that extracting local variables in a quick way is possible but not recommended. I guess because of the reasons given in the post mentioned in the first comment... – Jan Jun 07 '13 at 22:00
  • 2
    @Jan that's true, consider using another dictionary to store this subset in. If you have to just do it manually `a, c = d['a'], d['c']`. If you are beginning to use many variables (ie. `>=5`) then you should be using another dict) – jamylak Jun 07 '13 at 22:08
  • Making local variables for a small, static set of names is perfectly reasonable, and practical. It's only a bad idea when you're trying to create a whole bunch of variables, or name them dynamically. – abarnert Jun 07 '13 at 22:50

5 Answers5

5

You can use a generator expression for this:

>>> d = {'a':1,'b':2,'c':3}
>>> a, c = (d[key] for key in ['a', 'c'])
>>> a
1
>>> c
3

If you do this often enough that it's worth wrapping up in a function, that's trivial:

>>> def multiget(mapping, *keys):
...     return (d[key] for key in keys)
>>> a, c = multiget(d, 'a', 'c')

However, if you look carefully, you'll see that multiget is just operator.itemgetter uncurried, and it's usually a lot simpler to just use that:

>>> a, c = operator.itemgetter('a', 'c')(d)
abarnert
  • 354,177
  • 51
  • 601
  • 671
1

For educational purposes:

>>> d = {'a':1,'b':2,'c':3}
>>> globals().update((k, d[k]) for k in ('a', 'c'))
>>> a
1
>>> c
3

However never actually use something like this

jamylak
  • 128,818
  • 30
  • 231
  • 230
  • What? I loved your operator solution! Please get it back! (I was even about to up vote it) – TerryA Jun 07 '13 at 21:49
  • @Haidro I realised it was useless (could just use a list comprehension) and OP's question *seems* to be different. You may use it in your answer incase OP wanted that – jamylak Jun 07 '13 at 21:50
1

You almost had it :p

>>> a, c = dct['a'], dct['c']
>>> a
1
>>> c
3

Please don't use dict as a variable name :). It's already a built-in type and function, so you've just overridden it :p.

TerryA
  • 58,805
  • 11
  • 114
  • 143
1

operator.itemgetter is handy for this

d = {'a':1,'b':2,'c':3}
from operator import itemgetter
a, c = itemgetter('a', 'c')(d)
print a, c
1 3
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
iruvar
  • 22,736
  • 7
  • 53
  • 82
  • I didn't get to the `itemgetter` until the end of my answer, but I think it's usually the clearest answer, so… definite +1 here. – abarnert Jun 07 '13 at 21:54
  • @abarnert, thanks, but ..your answer's a heck of a lot more educational, +1 – iruvar Jun 08 '13 at 05:36
1

Like functional style ?

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

>>> a, c = map(d.get, ('a','c'))
>>> a, c
(1, 3)
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125