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


Groups > comp.lang.python > #40502

Pythonic way for retrieving value for a nested dictionary.

Newsgroups comp.lang.python
Date 2013-03-04 22:48 -0800
Message-ID <4a0888ba-396b-4b6e-af3e-7560962d6750@googlegroups.com> (permalink)
Subject Pythonic way for retrieving value for a nested dictionary.
From Lowly Minion <fruiz@twitter.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web