Pythonic way for retrieving value for a nested dictionary.

Lowly Minion fruiz at twitter.com
Tue Mar 5 07:48:51 CET 2013


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



More information about the Python-list mailing list