why this code is wrong?
for i,j in range(100),range(200,300):
print i,j
when I test this for statement I see this error
ValueError: too many values to unpack
but when I test
for i, j in range(2),range(2):
print i,j
every thing is correct!
range(2)gives a list [0, 1]. So, youri, jwill be fetched from first list and and then from the second list.So, your loop is similar to: –
Prints: –
Now, if you have
range(3)there, then it will fail, because,range(3)gives a 3-element list, which cannot be unpacked intwo loop variables.So, you cannot do: –
It will fail, giving you the error that you are getting.
Try using
zip, to zip your both list into one.: –zipconverts your lists into list of tuples with2elements in the above case, as you are zipping 2 lists.Similarly, if you
zipthree lists, you will get list of 3-elements tuple.