473,396 Members | 1,989 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,396 software developers and data experts.

Iterating two arrays at once

Hi there,

just trying to figure out how to iterate over two array without
computing the len of the array:

A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b

It should print:

1,4
2,5
3,6

Thanks !
Aug 29 '08 #1
4 1656
mathieu a écrit :
Hi there,

just trying to figure out how to iterate over two array without
computing the len of the array:

A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b

It should print:

1,4
2,5
3,6
for a, b in zip(A, B):
print a, b

or, using itertools (which might be a good idea if your lists are a bit
huge):

from itertools import izip
for a, b in izip(A, B):
print a, b
Aug 29 '08 #2
Am Fri, 29 Aug 2008 03:35:51 -0700 schrieb mathieu:>
A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b

It should print:

1,4
2,5
3,6
Hey,

zip is your friend:

for a,b in zip(A,B):
print a,b

does what you want. If you deal with big lists, you can use izip from
itertools, which returns a generator.

from itertools import izip
for a,b in izip(A,B):
print a,b

HTH

Matthias
Aug 29 '08 #3
On Aug 29, 12:46 pm, Matthias Bläsing <matthias.blaes...@rwth-
aachen.dewrote:
Am Fri, 29 Aug 2008 03:35:51 -0700 schrieb mathieu:>
A = [1,2,3]
B = [4,5,6]
for a,b in A,B: # does not work !
print a,b
It should print:
1,4
2,5
3,6

Hey,

zip is your friend:

for a,b in zip(A,B):
print a,b

does what you want. If you deal with big lists, you can use izip from
itertools, which returns a generator.

from itertools import izip
for a,b in izip(A,B):
print a,b
Thanks all !
Aug 29 '08 #4
mathieu a écrit :
(snip solution)
Thanks all !
FWIW, this has been discussed here *very* recently (a couple hours ago).
Look for a thread named "iterating over two arrays in parallel?", and
pay special attention to Terry Reedy's answer.
Aug 29 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: matthurne | last post by:
I need to send just an array to a function which then needs to go through each element in the array. I tried using sizeof(array) / sizeof(array) but since the array is passed into the function,...
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
9
by: Ben Rudiak-Gould | last post by:
Background: I have some structs containing std::strings and std::vectors of other structs containing std::strings and std::vectors of .... I'd like to make a std::vector of these. Unfortunately the...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
3
by: Robert Mark Bram | last post by:
Hi all, I have some code to iterate through nested associative arrays. Can anyone please tell me what I am doing wrong? :) var dealers = new Array(); var dealer1 = new Array(); dealer1 =...
3
by: Lie | last post by:
When you've got a nested loop a StopIteration in the Inner Loop would break the loop for the outer loop too: a, b, c = , , def looper(a, b, c): for a_ in a: for b_ in b: for c_ in c:...
4
by: mh | last post by:
I want to interate over two arrays in parallel, something like this: a= b= for i,j in a,b: print i,j where i,j would be 1,4, 2,5, 3,6 etc.
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.