try:
# python 3 maybe?
from functools import reduce
except ImportError:
# assume python 2...
pass
reduce(dict.__getitem__, k, d)
A bit of explanation was requested: reduce(fn, [a1, a2, a3...], b) translates into: fn(... fn(fn(fn(b, a1), a2), a3) ...). So, it pairs some "accumulator" value with consecutive values from the sequence, passes that to the function passed in and then uses that to update the "accumulator" value.
So our "accumulator" value is top-level dictionary, which we pass as the first ("self") argument into dictionary method __getitem__
, which implements subscript operator.
As noted in the comments below you can use operator.getitem
in place dict.__getitem__
to generalize to other datatypes that support indexing such as lists, tuples, strings...