Have you tried writing your own helper function `dig(d, *args)`? It should only take a few lines, and you can easily put that in a library of "utility" functions you carry around with you from project to project. Not everything needs to be built in!

On Thu, Aug 29, 2019 at 1:53 PM None via Python-ideas <python-ideas@python.org> wrote:
I did some searching to see if this has already been proposed before, but didn't find any evidence that it has.  If so, let me know and I'll go away :)

One of the tasks that I encounter frequently enough is retrieving a nested key from a nested collection of dictionaries (a dictionary of dictionaries that can be any number of layers deep).  There are multiple ways of tackling this, normally done with `reduce()`, iterative looping or chained `get()`s and then handling `None`, `KeyError` or `AttributeError` appropriately.  I'd like to avoid this extra code and logic and have a built-in method to facilitate this common data access pattern.

I'd like to propose that we add a `dig()` method to dictionaries analogous to Ruby's `dig()` method for Ruby Hashes (reference: https://ruby-doc.org/core-2.6.0.preview2/Hash.html#method-i-dig).  Initially, I'd suggest that we only support nested dictionaries, but we could also support lists and other collection types as Ruby does if we really want to.  Similar to the existing `get()` method on dictionaries, I'd propose that the method return `None` if any of the keys in the chain is not found, avoiding `KeyError`.

Thoughts?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/QFL76SGB562Q6C5N6ZWGNUXDIU5N7MKK/
Code of Conduct: http://python.org/psf/codeofconduct/


--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him/his (why is my pronoun here?)