Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #141 Iterate in sequence over two lists

Iterate in sequence over the elements of the list items1 then items2. For each iteration print the element.

for (let item of items1) console.log(item)
for (let item of items2) console.log(item)
items1.concat(items2).forEach(console.log)
;; for side effects
(doseq [x (concat items1 items2)]
   (println x))
void print_seq(const auto& ...xs)
{
 (std::for_each(std::begin(xs), std::end(xs), 
           [](const auto& x) { std::cout << x; }), ...);
}

 std::list xs { "hi", "there", "world" }, ys { "lo" "thar" };

 print_seq(xs, ys);
using System;
using System.Linq;
foreach (var item in items1.Concat(items2))
{
    Console.WriteLine(item);
}
items1.ForEach(Console.WriteLine);
items2.ForEach(Console.WriteLine);
import std.range;
import std.algorithm.iteration;
import std.stdio;
chain(items1, items2).each!writeln;
items1.forEach(print);
items2.forEach(print);
def main(items1, items2) do
  Enum.each(items1, &IO.puts/1)
  Enum.each(items2, &IO.puts/1)
end
items1 ++ items2 |> Enum.each(&IO.puts/1)
print *,items1,items2
for _, v := range items1 {
	fmt.Println(v)
}
for _, v := range items2 {
	fmt.Println(v)
}
mapM_ print $ items1 ++ items2
items1.forEach(System.out::println);
items2.forEach(System.out::println);
import static java.lang.System.out;
for (E e : items1) out.println(e);
for (E e : items2) out.println(e);
import java.util.stream.Stream;
Stream.concat(
	items1.stream(), 
	items2.stream()
).forEach(System.out::println);
(map nil #'print (append items1 items))
# beginner style
foreach( $items1 as $item ) print "$item\n";
foreach( $items2 as $item ) print "$item\n";

# five times faster
print implode("\n", $items1)."\n".implode("\n", $items2)."\n" ;
for i := 0 to items1.Count-1 do writeln(items1[i]);
for i := 0 to items2.Count-1 do writeln(items2[i]);
print for @items1, @items2;
for x in items1 + items2:
    print(x)
from itertools import chain
for x in chain(items1, items2):
    print(x)
items1.chain(items2).each{|item| puts item}
[items1, items2].each{|ar| ar.each{|item| p item }}
for i in item1.iter().chain(item2.iter()) {
    print!("{} ", i);
}
(map (lambda (x)
       (display x)
       (newline))
     (append items1 items2))

New implementation...
< >
BBaz