Loop over multiple arrays simultaneously: Difference between revisions

Added Python procedural approach, added headings, fixed typo
(Add Uiua)
(Added Python procedural approach, added headings, fixed typo)
(One intermediate revision by the same user not shown)
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}}==
Line 4,670 ⟶ 4,689:
cC3
</pre>
 
=={{header|Vim Script}}==
<syntaxhighlight lang="vimscript">
let a1 = ['a', 'b', 'c']
let a2 = ['A', 'B', 'C']
let a3 = [1, 2, 3]
for i in range(0, len(a1) - 1)
echo a1[i] .. a2[i] .. a3[i]
endfor
</syntaxhighlight>
 
If either a2 or a3 have fewer list items than a1, Vim will error with the message:
<pre>E684: List index out of range</pre>
If either a2 and/or a3 have more list items than a1, list items with index > len(a1) - 1 are ignored.
 
=={{header|Visual FoxPro}}==
31

edits