views:

41

answers:

1

I have vsort and vsorta, both lists with equal numbers of items that should be right next to each other (about 250 elements per list). I want to print them as parallel columns, like so:

>>> for x,y in vsort,vsorta:
...     print x, y
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> 

Is there a way around this error?

+7  A: 

Try:

for x, y in zip(vsort, vsorta):
       print x, y

zip takes some number of lists and makes them into one list of tuples.

David Winslow
Or `izip` from the `itertools` module (in Python 2.x), which creates a generator instead of a whole new list and thus uses less memory.
David Zaslavsky
Thanks. Zip worked fine -- I'll try izip if I ever run into this problem again. I wish I knew how all of these doohickeys worked, though...
old Ixfoxleigh