Loop over multiple arrays simultaneously: Difference between revisions

Added Python procedural approach, added headings, fixed typo
(Added Vim Script solution)
(Added Python procedural approach, added headings, fixed typo)
Line 3,650:
 
=={{header|Python}}==
=== Using <tt>zip()</tt>: ===
<syntaxhighlight lang="python">>>> print ( '\n'.join(''.join(x) for x in
zip('abc', 'ABC', '123')) )
Line 3,660:
the shortest one.
 
=== Using <tt>map()</tt>: ===
<syntaxhighlight lang="python">>>> print(*map(''.join, zip('abc', 'ABC', '123')), sep='\n')
aA1
Line 3,669:
<tt>None</tt> items; <tt>map()</tt> in Python 3.x stops after the shortest one.
 
=== Using <tt>itertools.imap()</tt> <small>(Python 2.x):</small> ===
<syntaxhighlight lang="python">from itertools import imap
 
Line 3,676:
 
imap(join3,'abc','ABC','123')</syntaxhighlight>
If lists are differntdifferent lengths, <tt>imap()</tt> stops after
the shortest is exhausted.
 
Line 3,692:
>>></syntaxhighlight>
(The Python 2.X equivalent is itertools.izip_longest)
 
=== Traditional procedural approach ===
<syntaxhighlight lang="python">
a1 = ['a', 'b', 'c']
a2 = ['A', 'B', 'C']
a3 = [1, 2, 3]
for i in range(len(a1)):
print(a1[i] + a2[i] + str(a3[i]))
</syntaxhighlight>
{{out}}
<pre>
aA1
bB2
cC3
</pre>
 
If either list a2 or list a3 have fewer items than list a1, the result will be:
<pre>IndexError: list index out of range</pre>
If either list a2 and/or list a3 have more items than list a1, their extra items are ignored.
 
=={{header|Quackery}}==
31

edits