Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40504
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <davea@davea.name> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'exist,': 0.07; 'nested': 0.07; 'none)': 0.07; 'default)': 0.09; 'dict': 0.09; 'exception,': 0.09; 'exists,': 0.09; 'get.': 0.09; 'lookup': 0.09; 'throw': 0.09; 'advance': 0.10; "'a',": 0.16; "'b'": 0.16; "'b',": 0.16; "'c',": 0.16; '(rather': 0.16; 'looping': 0.16; 'namedtuple': 0.16; 'none),': 0.16; 'pythonic': 0.16; 'wrote:': 0.17; 'typical': 0.17; 'keys': 0.22; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'i.e.': 0.27; 'list:': 0.27; "doesn't": 0.28; 'dictionary': 0.29; 'key,': 0.29; 'way?': 0.29; 'wrap': 0.29; "i'm": 0.29; 'print': 0.32; 'getting': 0.33; 'like:': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'data,': 0.35; 'doing': 0.35; 'something': 0.35; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'thank': 0.36; 'item': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'most': 0.61; 'first': 0.61; 'is.': 0.62; 'within': 0.64; 'received:74.208': 0.71; "'2',": 0.84; 'dict.': 0.84; 'received:74.208.4.194': 0.84; 'subject:value': 0.84; 'was:': 0.91 |
| Date | Tue, 05 Mar 2013 02:20:51 -0500 |
| From | Dave Angel <davea@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Pythonic way for retrieving value for a nested dictionary. |
| References | <4a0888ba-396b-4b6e-af3e-7560962d6750@googlegroups.com> |
| In-Reply-To | <4a0888ba-396b-4b6e-af3e-7560962d6750@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:37P9H1osAIzG6DrXGO19ROyLqmNp4gIFLfAKRibK7x+ 75YRY07wFMcYrN5kgy2u54sSHOSDA7nn/DsWQ3Xt0b814uL8Mm eKPOWhFsMdx7adcECZon9L93b+Zb6uI7AREUGm3wA4LYNDoxP3 txkrhqn/J/8maaCyXx+Yxq4bvqxqIIj2ki999kPtbl9TrPDqw7 xw2uGDpHQiicUUah/Szs6haxGgyhKM1DFQDaYb3JjAuyk7G/jV 5j7EYUNq5JnjC2+rkSvWRm14hQxzikIKwO2BMqaftRnCdONBml mjooEEFz8QXNy3ayNQ1mT7Wb9ly+Hfh+zU3upGo03RwjHDMUA= = |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2868.1362468065.2939.python-list@python.org> (permalink) |
| Lines | 47 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1362468065 news.xs4all.nl 6955 [2001:888:2000:d::a6]:54209 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:40504 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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