Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40502 > unrolled thread

Pythonic way for retrieving value for a nested dictionary.

Started byLowly Minion <fruiz@twitter.com>
First post2013-03-04 22:48 -0800
Last post2013-03-05 02:20 -0500
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Pythonic way for retrieving value for a nested dictionary. Lowly Minion <fruiz@twitter.com> - 2013-03-04 22:48 -0800
    Re: Pythonic way for retrieving value for a nested dictionary. Dave Angel <davea@davea.name> - 2013-03-05 02:20 -0500

#40502 — Pythonic way for retrieving value for a nested dictionary.

FromLowly Minion <fruiz@twitter.com>
Date2013-03-04 22:48 -0800
SubjectPythonic way for retrieving value for a nested dictionary.
Message-ID<4a0888ba-396b-4b6e-af3e-7560962d6750@googlegroups.com>
For a typical dict:

i.e. 
d = { '1': 'a', '2', 'b' }

I might use something like:

d.get('1', None)

To get the value of 1.

What would be the most pythonic way of getting a nested value of a dictionary within a list:

some_list = [{ 'item': { 'letter': 'b',
                                  'word': 'bar' }},
                    { 'item': { 'letter': 'c',
                                  'word': 'charcoal' }}]

Currently I am looping through the list as such:

for item in some_list:
  print item['item']['letter']

One other method explored was:
item.get('item').get('letter')

Is a double get the best way? Doesn't seem right to me.

Thank you in advance for your help

[toc] | [next] | [standalone]


#40504

FromDave Angel <davea@davea.name>
Date2013-03-05 02:20 -0500
Message-ID<mailman.2868.1362468065.2939.python-list@python.org>
In reply to#40502
On 03/05/2013 01:48 AM, Lowly Minion wrote:
> For a typical dict:
>
> i.e.
> d = { '1': 'a', '2', 'b' }
>
> I might use something like:
>
> d.get('1', None)
>
> To get the value of 1.
>
> What would be the most pythonic way of getting a nested value of a dictionary within a list:
>
> some_list = [{ 'item': { 'letter': 'b',
>                                    'word': 'bar' }},
>                      { 'item': { 'letter': 'c',
>                                    'word': 'charcoal' }}]
>
> Currently I am looping through the list as such:
>
> for item in some_list:
>    print item['item']['letter']
>
> One other method explored was:
> item.get('item').get('letter')
>
> Is a double get the best way? Doesn't seem right to me.
>
> Thank you in advance for your help
>


I'm not sure what the puzzle is.  Use [key] if you know the key exists, 
and use get( key, default) if you don't.  But be aware that if the first 
key doesn't exist, then the default had better be a dict (rather than 
something like None), or the second lookup will throw an exception, even 
if you use get.

One other possibility, depending on just what you're doing with the 
data, is to wrap it in a try/catch.

However, if you know exactly what the keys are, perhaps you should use a 
namedtuple instead of a dict.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web