By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

Loop three lists at the same time?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.