3

Is there a recursive itemgetter in python. Let's say you have an object like

d = {'a': (1,2,3), 'b': {1: (5,6)}}

and I wanted to get the first element of the tuple from d['a']? As far as I can tell itemgetter will only do one level, i.e. get me a or b or both.

Is there some clever way of combining itertools with itemgetter to produce the desired result.

So basically what I want to be able to call is

from operator import itemgetter
d = {'a': (1,2), 'b': (4, (5,))}
itemgetter({'a': 0})(d) --> 1
itemgetter({'b': 0})(d) --> 4
itemgetter({'b': {1: 0}})(d) --> 5

d = {'a': {'b': (1,2,3)}}
itemgetter({'a': {'b': 2}})(d) --> 3
2
  • ok, so there is this (pypi.python.org/pypi/jmespath) which does what I want, but is not part of the standard library Commented Oct 26, 2016 at 9:31
  • I don't know if it is clever or not, but a1 = lambda d: itemgetter(1)(itemgetter('a')(d)) will do as requested, i.e. a1(d) will "get the first element of the tuple from d['a']". Commented Jun 28, 2020 at 9:01

1 Answer 1

10

I don't like 'clever' ways. Obvious is better.

You can very easily write a getter that iterates along a list of subscripts:

def getter(x, *args):
    for k in args:
        x = x[k]
    return x

>>> d = {'a': (1,2,3), 'b': {1: (5,6)}}
>>> getter(d, 'a', 0)
1
Sign up to request clarification or add additional context in comments.

1 Comment

Explicit is better than implicit... except when it isn't. It's all full of... well, it's basically tradeoffs all the way down. And yes, you can write that yourself. But you can do so for tons of the syntactic sugar in core Python. Decorators?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.