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


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

Re: Loop thru the dictionary with tuples

Started byNed Batchelder <ned@nedbatchelder.com>
First post2014-05-25 09:58 -0400
Last post2014-05-25 09:58 -0400
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#72008 — Re: Loop thru the dictionary with tuples

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-05-25 09:58 -0400
SubjectRe: Loop thru the dictionary with tuples
Message-ID<mailman.10296.1401026296.18130.python-list@python.org>
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.

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

[toc] | [standalone]


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


csiph-web