too many values to unpack', iterating over a dict. key=string, value=list
Python 3
Use items()
.
for field, possible_values in fields.items():
print(field, possible_values)
Python 2
Use iteritems()
.
for field, possible_values in fields.iteritems():
print field, possible_values
See this answer for more information on iterating through dictionaries, such as using items()
, across Python versions.
For reference, iteritems()
was removed in Python 3.
Too many values to unpack in Python dictionary value split()?
If you are looking for pairs of keys and values of a dictionnary, you have to use .items()
.
for key, value in spee_dict.items():
If you do not, you are just iterating trough the keys. So, as the key is not a tuple, this raises an error because you can not unpack it.
ValueError: too many values to unpack in Python Dictionary
You are looping over a dictionary:
for key, role in relatives:
but that only yields keys, so one single object at a time. If you want to loop over keys and values, use the dict.items()
method:
for key, role in relatives.items():
On Python 2, use the dict.iteritems()
method for efficiency:
for key, role in relatives.iteritems():
too many values to unpack iterating over dictionary indexed by tuples
I don't understand your purpose.
But function select's error is:
- The studentPerf.keys() return a iterable with 3-tuple
- your code
for key, index in
just unpack two item
you should:
for index0, index1, index2 in studentPerf.keys():
orfor key, *index in studentPerf.keys()
. this way the index would be a 2-tuple
Too many values to unpack: for loop with nested dictionaries
There are two issues with your code:
- You need to iterate over
dictByLang[askedLanguage].items()
, rather than justdictByLang[askedLanguage]
(which only iterates over the keys). - The
'type'
key in yourfor
loop needs to have a capital T.
So, the for
loop should look like the following:
for key, val in dictByLang[askedLanguage].items():
print(key, val['auth'], val['Type'], val['sold'])
ValueError: too many values to unpack (expected 2) when passing values from dictionary
You're making PList
a tuple of lists. You want a list of tuples. Use zip()
for that.
PList = list(zip(self._fi_apps_list, man_list))
Too many values to unpack (expected 2) in Python while using dictionary function
Your question is not clear enough, but I think this is what's missing :
The items in d
should be presented as a sequence of two values (like [('aa', 3), ('bb', 4), ...]
. to be able to get assign it to a the tuple word, mean
which is composed of 2 elements.
and when using a dict
the method items()
returns something similar to what you need which is of type dict_items()
.
for word, mean in d.items() :
print(word, "<------>", mean)
How to iterate over a dictionary?
foreach(KeyValuePair<string, string> entry in myDictionary)
{
// do something with entry.Value or entry.Key
}
Python3 ValueError - Too many values to unpack while iterating over a list
Use zip
and iterate over two slices of the list
[(f, l) for f, l in zip(names[:-1], names[1:]]
Iterating over dictionaries using 'for' loops
key
is just a variable name.
for key in d:
will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following:
For Python 3.x:
for key, value in d.items():
For Python 2.x:
for key, value in d.iteritems():
To test for yourself, change the word key
to poop
.
In Python 3.x, iteritems()
was replaced with simply items()
, which returns a set-like view backed by the dict, like iteritems()
but even better.
This is also available in 2.7 as viewitems()
.
The operation items()
will work for both 2 and 3, but in 2 it will return a list of the dictionary's (key, value)
pairs, which will not reflect changes to the dict that happen after the items()
call. If you want the 2.x behavior in 3.x, you can call list(d.items())
.
Related Topics
How to Validate Ip Address in Python
Authenticate from Linux to Windows SQL Server with Pyodbc
How to Print a String at a Fixed Width
What's the How to Install Pip, Virtualenv, and Distribute for Python
Python: Ignore 'Incorrect Padding' Error When Base64 Decoding
Difference Between Exit() and Sys.Exit() in Python
Permissionerror: [Errno 13] in Python
Iterate Over Model Instance Field Names and Values in Template
Appending the Same String to a List of Strings in Python
How to Make a Sunburst Plot in R or Python
"Getaddrinfo Failed", What Does That Mean
Pandas: Rolling Mean by Time Interval