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


Groups > comp.lang.python > #72044

Re: Loop thru the dictionary with tuples

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '(python': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrote': 0.14; 'dict': 0.16; 'igor': 0.16; 'iterating': 0.16; 'keys.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; '>>>': 0.22; 'header :User-Agent:1': 0.23; 'error': 0.23; 'large,': 0.24; 'received:comcast.net': 0.24; 'fine': 0.24; 'this:': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; 'gives': 0.31; 'getting': 0.31; 'bad.': 0.31; 'produces': 0.31; 'stuff': 0.32; 'subject:the': 0.34; 'subject:with': 0.35; 'but': 0.35; 'doing': 0.36; 'hi,': 0.36; 'thank': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'dave': 0.60; 'you.': 0.62; '"too': 0.84; 'avoid.': 0.84; 'batchelder': 0.84; 'dict,': 0.84; 'want:': 0.84; 'angel': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Loop thru the dictionary with tuples
Date Sun, 25 May 2014 22:13:57 -0400
References <CA+FnnTwpQKTyvROYq32YsDfwDooDXRbRg-SYGGhFs1T1D9o_Ow@mail.gmail.com> <llsstb$1sa$1@ger.gmane.org> <llu7ni$dph$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
In-Reply-To <llu7ni$dph$1@ger.gmane.org>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10326.1401070507.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1401070507 news.xs4all.nl 2899 [2001:888:2000:d::a6]:44270
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72044

Show key headers only | View raw


On 5/25/14 10:09 PM, Dave Angel wrote:
> Ned Batchelder <ned@nedbatchelder.com> Wrote in message:
>> On 5/25/14 8:55 AM, Igor Korot wrote:
>>> Hi, ALL,
>>> I have a following data structure:
>>>
>>> my_dict[(var1,var2,var3)] = None
>>> my_dict[(var4,var5,var6)] = 'abc'
>>>
>>> What I'm trying to do is this:
>>>
>>> for (key,value) in my_dict:
>>>       #Do some stuff
>>>
>>> but I'm getting an error "Too many values to unpack".
>>>
>>> What am I doing wrong?
>>>
>>> Thank you.
>>>
>>
>> You want:
>>
>>       for key, value in my_dict.items():  # or .iteritems()
>>
>> Iterating over a dictionary gives you its keys.  items() will give you
>> key,value pairs.
>
>
> Or, if the dict is large,  you might want
>
> for key in my_dict:
>      value = my_dict [key]
>      ...

For a large dict, .iteritems (Python 2) or .items (Python 3) are 
perfectly fine and don't have a cost to avoid.   .items (Python 2) 
produces a list, which would be bad.

-- 
Ned Batchelder, http://nedbatchelder.com

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


Thread

Re: Loop thru the dictionary with tuples Ned Batchelder <ned@nedbatchelder.com> - 2014-05-25 22:13 -0400

csiph-web