0

Just like I'm allowed to do this:

(EDIT - I'm NOT allowed to do this either, sorry messed up, but anyway I guess my question is more on the lines of how to go about doing parallel assignments in a loop)

for (x,i) in ([0,1,2],[3,4,5]):
  # do something

why am I not allowed to do this in a for each loop (different list length)

 for (x,i) in ([4,6,5,7,8,9],[1,2,3]):
   # do something

I know you get a "ValueError: too many values to unpack", but why doesn't the loop stop when i is done with its iteration?

A real use of this would be for something like this that I was trying to do

for (keys,i) in (sorted(dic.keys(),key=custom_sort), range(10)):
  print dic[keys]

where I'm sorting the dictionary as well as printing only the first 10 top results (assume the dictionary has hundreds of keys) - I only wanted the top 10 results.

Since this kind of syntax is not allowed anyway what could be the next best thing to do?

thanks

5 Answers 5

2

For this kind of stuff, itertools is your friend.

In your case, itertools.islice() will do the trick:

import itertools
for key in itertools.islice(sorted(dict.keys(), key=custom_sort), 10):
    # Only the first 10 keys are processed.

Alternatively, if you need the index as well, use zip or itertools.izip:

for i, key in zip(range(10), sorted(dict.keys(), key=custom_sort)):
    # i will be 0, 1, ..., 9
1
  • +1: You clearly know your Python recipes far better than I do!
    – johnsyweb
    Jul 30, 2011 at 9:57
2

You can't do what you do in your first example - it gives exactly the same error. So the error isn't from the lists being different lengths at all.

I think what you mean to do is this:

for (x,i) in zip([0,1,2],[3,4,5]):

which works just as well with lists of different lengths

>>> for x, i in zip([4,6,5,7,8,9],[1,2,3]):
...   print x, i
... 
4 1
6 2
5 3
0
1

You are not allowed to do this:

for (x,i) in ([0,1,2],[3,4,5]):
  # do something
3
  • I swear i tried this in interpretor before posting the question and now it doesn't work, damn! the laws of programming be a harsh mistress
    – Raja
    Jul 30, 2011 at 9:51
  • so anyway, what could be the best way to go about parallel assignment in a loop?
    – Raja
    Jul 30, 2011 at 9:52
  • 1
    Is this an answer or a comment? Jul 30, 2011 at 9:53
1
for (x,i) in ([0,1,2],[3,4,5]):
    # do something

Will raise:

ValueError: too many values to unpack

It looks like you are trying to do this:

>>> import itertools as i
>>> first,second = [4,6,5,7,8,9],[1,2,3]
>>> for x,i in i.izip(first, second):
...     print x, i
... 
4 1
6 2
5 3

Take a look at itertools, specifically izip(). There's a wealth of goodness therein... for instance, the islice() function, in Ferdinand Beyer's answer and other recipes.

0

for iterates over the elements in the container. Each element is a single list with greater than two elements.

Try this:

(x, y) = ([0, 1, 2], [3, 4, 5])

Which is what you want. No need for the for.

Note that your first example that you claim works doesn't actually. You can't do:

for (x, y) in ([0, 1, 2], [3, 4, 5]):
    pass

You get the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.