I followed this post to create functions that get and set values into a nested dictionary given a list of keys: Access nested dictionary items via a list of keys?
# get a dict value with a list of nested keys
def getFromDict(dataDict, mapList):
return reduce(lambda d, k: d[k], mapList, dataDict)
# set a dict value with a list of nested keys
def setInDict(dataDict, mapList, value):
getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value
How would this be done Pythonically (2.x) using list comprehension or otherwise while avoiding reduce?