Loop thru the dictionary with tuples

Dave Angel davea at davea.name
Sun May 25 22:09:28 EDT 2014


Ned Batchelder <ned at 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]
    ...


-- 
DaveA




More information about the Python-list mailing list