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

Idiom #163 Print list elements by group of 2

Print all the list elements, two by two, assuming list length is even.

i := Low(L);
while (i < High(L)) do
begin
  writeln(L[i],', ', L[i+1]);
  Inc(i,2);
end;
import std.range;
list.chunks(2).each!writeln;
import "fmt"
for i := 0; i+1 < len(list); i += 2 {
	fmt.Println(list[i], list[i+1])
}
everySecond :: [a] -> [a]
everySecond [] = []
everySecond (_:[]) = []
everySecond (_:x:xs) = x : everySecond xs
everySecond' :: [a] -> [a]
everySecond' = everySecond . (undefined :)

mapM_ print (zip (everySecond list) (everySecond' list))
foreach(array_chunk($list, 2) as $x) {
    echo $x[0], ' ', $x[1], PHP_EOL;
}
for x in zip(list[::2], list[1::2]):
    print(x)
list.each_slice(2){|slice| p slice}
for pair in list.chunks(2) {
    println!("({}, {})", pair[0], pair[1]);
}
for (pair <- list.grouped(2))
  println(s"(${pair(0)}, ${pair(1)})")

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Bzzzzzzzzzz

Related idioms