Using for... for multiple lists

H

Harlin Seritt

I am using the following code:

for i, f in filelist, self.checkbox:
if f.get():
print i

I am getting an error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "install.py", line 123, in select_stage
for i, f in filelist, self.checkbox:
ValueError: unpack list of wrong size

Both filelist and self.checkbox are the same size (4 items btw). Is
there any way to get this done?

Thanks,

Harlin
 
D

Diez B. Roggisch

Both filelist and self.checkbox are the same size (4 items btw). Is
there any way to get this done?

Use zip:

a, b = [1,2,3], ['a', 'b', 'c']
for i,j in zip(a,b):
print i,j

Or from itertools izip - which should be slightly more performant and less
memory consuming.
 
H

Heiko Wundram

for i, f in filelist, self.checkbox:
if f.get():
print i

Use:

for i, f in zip(filelist,self.checkbox):
<bla>

--
--- Heiko.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQBCKvUAf0bpgh6uVAMRAgqQAJ9LksE8xHaJCO6N+udbd+kyrlsJ1gCdENTD
0yP3wmYLzbOAq2EI3BJniOE=
=53Rm
-----END PGP SIGNATURE-----
 
G

gene.tani

If sequences are not same length:

zip truncates to length of shortest input sequence

map(None, seq1, seq2) pads to length of longest seq.

(can't remember what itertools.izip() does)
 
H

Harlin Seritt

Actually the sequences are of the same length. Forgot to mention that.

thanks,

Harlin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,222
Messages
2,571,138
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top