dict.get_deep()

Marco Sulla Marco.Sulla.Python at gmail.com
Sat Apr 2 16:44:22 EDT 2022


A proposal. Very often dict are used as a deeply nested carrier of
data, usually decoded from JSON. Sometimes I needed to get some of
this data, something like this:

data["users"][0]["address"]["street"]

What about something like this instead?

data.get_deep("users", 0, "address", "street")

and also, instead of this

try:
    result = data["users"][0]["address"]["street"]
except KeyError, IndexError:
    result = "second star"

write this:

data.get_deep("users", 0, "address", "street", default="second star")

?


More information about the Python-list mailing list