I recommend you take at look at the "toolz" library, which provides
assorted APIs to help in data structure manipulation:
https://toolz.readthedocs.io/en/latest/index.html

Especially this function:
https://toolz.readthedocs.io/en/latest/api.html#toolz.dicttoolz.get_in

Regards

Antoine.


On Thu, 29 Aug 2019 15:43:43 -0000
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-le...@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/
> 


_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/X6WFTNRAT5BOHUW3PJHD46PHQC574KWN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to