Logo

Programming-Idioms

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.

list.each_slice(2){|slice| p slice}
#include <stdio.h>
for (unsigned i = 0; i < sizeof(list) / sizeof(list[0]); i += 2)
	printf("%d, %d\n", list[i], list[i + 1]);
(doseq [xs (partition 2 list)]
  (apply println xs))
using System;
using System.Linq;
foreach (var chunk in list.Chunk(2))
{
    Console.WriteLine(string.Join(' ', chunk));
}
using System.Collections.Generic;
for(int i = 0; i < list.Count; i += 2) {
  Console.WriteLine(string.Format("{0}, {1}", list[i], list[i + 1]));
}
import std.range;
list.chunks(2).each!writeln;
for(int i = 0; i < list.length; i += 2) {
  print("${list[i]}, ${list[i + 1]}");
}
write (*,'(2I8,:," ")') list
import "fmt"
for i := 0; i+1 < len(list); i += 2 {
	fmt.Println(list[i], list[i+1])
}
pair :: [a] -> [(a, a)]
pair [] = []
pair (x:[]) = error "List had odd length"
pair (x:y:xs) = (x, y) : pair xs

mapM_ print (pair list)
everySecond :: [a] -> [a]
everySecond [] = []
everySecond (_:[]) = []
everySecond (_:x:xs) = x : everySecond xs
everySecond' :: [a] -> [a]
everySecond' = everySecond . (undefined :)

mapM_ print (zip (everySecond list) (everySecond' list))
for (let index = 0; index < list.length; index = index + 2) {
  console.log(list[index], list[index + 1])
}
for(int i = 0; i < list.length; i += 2) {
  System.out.println(list[i] + ", " + list[i + 1]);
}
import static java.lang.System.out;
import static java.util.stream.IntStream.range;
range(0, list.size())
    .filter(i -> i % 2 == 0)
    .mapToObj(i -> list.get(i) + ", " + list.get(i + 1))
    .forEach(out::println);
foreach(array_chunk($list, 2) as $x) {
    echo $x[0], ' ', $x[1], PHP_EOL;
}
i := Low(L);
while (i < High(L)) do
begin
  writeln(L[i],', ', L[i+1]);
  Inc(i,2);
end;
use List::Util qw(pairs);
foreach (pairs @list) {
   print "@$_\n";
}
from itertools import tee
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

for a, b in pairwise(list):
    print(a, b)
for x in zip(list[::2], list[1::2]):
    print(x)
from itertools import batched
for x in batched(a, 2): print(x)
from itertools import zip_longest as z
i = iter(a)
for x in z(i, i): print(x)
for pair in list.chunks(2) {
    println!("({}, {})", pair[0], pair[1]);
}
for (pair <- list.grouped(2))
  println(s"(${pair(0)}, ${pair(1)})")
(let print-pairs ((l list))
  (if (null? l)
      '()
      (begin
        (display (car l))
        (display " ")
        (display (cadr l))
        (newline)
        (print-pairs (cddr l)))))
list pairsDo: [:a :b |
  Transcript showln: 'a: ' , a , ' b: ' , b].
Dim ItemList As New List(Of String)(New String() {"one", "one", "two", "two", "three", "three"})
For x = 0 To ItemList.Count - 1 Step 2
    Console.WriteLine(ItemList(x) & vbTab & ItemList(x + 1))
Next

New implementation...
< >
Bzzzzzzzzzz