473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1127
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
25
by: skull | last post by:
Hi everybody, it is my first post in this newsgroup. I am a newbie for python though I have several years development experience in c++. recently, I was stumped when I tried to del item of a list...
6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
12
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
6
by: Horta | last post by:
Hi folks, Suppose I have to loop over 3 lists being the same size at the same time and order. How can I do that without using the range() function or whatever indexing? Example using range: ...
1
by: rshepard | last post by:
Haven't found an answer to my question in the books and other docs I have available, so I am asking here. I have three lists of data retrieved from database tables. I want to cycle through all...
23
by: Sacred Heart | last post by:
Hi, I'm new to Python and have come across a problem I don't know how to solve, enter com.lang.python :) I'm writing some small apps to learn the language, and I like it a lot so far. My...
3
by: Brad | last post by:
Hi folks, I'm still fairly new to programming in python and programming in general. A friend of mine is in a CompSci 101 course and was working on a slider game when he encountered a problem. We...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.