473,581 Members | 2,424 Online

# 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 1130
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...@gmai l.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.

Coko

Nov 13 '07 #3
En Tue, 13 Nov 2007 07:46:09 -0300, Davy <zh*******@gmai l.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...@gmai l.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+lis t3:
.... print i
....

Nov 14 '07 #5
In article <11************ **********@v23g 2000prn.googleg roups.com>,
Davy <zh*******@gmai l.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 thread has been closed and replies have been disabled. Please start a new discussion.