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

Idiom #143 Iterate alternatively over two lists

Iterate alternatively over the elements of the list items1 and items2. For each iteration, print the element.

import std.range;
import std.algorithm.iteration;
import std.stdio;
roundRobin(items1, items2).each!writeln;
for i := 0; i < len(items1) || i < len(items2); i++ {
	if i < len(items1) {
		fmt.Println(items1[i])
	}
	if i < len(items2) {
		fmt.Println(items2[i])
	}
}
[items1, items2].transpose.each{|ar| 
  ar.each{|item| p item }
}
(map (lambda (x)
       (display x)
       (newline))
     (foldr append '()
            (map list items1 items2)))

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

Idiom created by

BBaz

Related idioms