471,334 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

Loop three lists at the same time?

Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy

Nov 13 '07 #1
5 1085
Davy wrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
Hello,

the zip function?
>>list1 = [1,2,3]
list2 = [4,5,6]
list3 = [7,8,9]
for a,b,c in zip(list1,list2,list3):
.... print a, b, c
....
1 4 7
2 5 8
3 6 9

hth
j.
Nov 13 '07 #2
On Nov 13, 11:46 am, Davy <zhushe...@gmail.comwrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
What exactly do you mean?

Are you trying to loop them together with the same ´count´. "for loop
in zip(1,2,3) kind of thing or something else.

Please either post a more descriptive question, or your current code.

Coko

Nov 13 '07 #3
En Tue, 13 Nov 2007 07:46:09 -0300, Davy <zh*******@gmail.comescribió:
I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?
Try zip or itertools.izip:

pyL1 = ['a','b','c']
pyL2 = [1, 2, 3]
pyL3 = ['I', 'II', 'III']
pyfrom itertools import izip
pyfor x,y,z in izip(L1,L2,L3):
.... print x,y,z
....
a 1 I
b 2 II
c 3 III

--
Gabriel Genellina

Nov 13 '07 #4
On 11 13 , 6 46 , Davy <zhushe...@gmail.comwrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
Maybe you just need this:-)
>>list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
for i in list1+list2+list3:
.... print i
....

Nov 14 '07 #5
In article <11**********************@v23g2000prn.googlegroups .com>,
Davy <zh*******@gmail.comwrote:
Hi all,

I have three lists with the same length. Is there any method to loop
the three lists without a loop counter?

Best regards,
Davy
Davy,

Look at the "zip" function,

<http://docs.python.org/lib/built-in-funcs.html>

Look near the bottom of the page.

Example:
zip([1,2,3], [4,5,6], [7,8,9])
==[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Nov 14 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by flupke | last post: by
25 posts views Thread by skull | last post: by
6 posts views Thread by Ravi | last post: by
73 posts views Thread by Claudio Grondi | last post: by
6 posts views Thread by Horta | last post: by
1 post views Thread by rshepard | last post: by
23 posts views Thread by Sacred Heart | last post: by
3 posts views Thread by Brad | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.