0

This function is for two keys:

def get(keyA, keyB)
    return mydict[keyA][keyB]

How can I write the following function for variable number of keys?

def get(*keys)
    return mydict[keys[0]][keys[1]][keys[3]]...
Kadir
  • 1,345
  • 3
  • 15
  • 25

1 Answers1

1

Iterate keys, and assign the item as you iterate:

def get(*keys):
    ret = mydict
    for key in keys:
        ret = ret[key]
    return ret
falsetru
  • 357,413
  • 63
  • 732
  • 636