This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #143 Iterate alternatively over two lists
Iterate alternatively over the elements of the lists items1 and items2. For each iteration, print the element.
Explain what happens if items1 and items2 have different size.
- Clojure
- C#
- D
- Fortran
- Go
- Go
- Haskell
- Haskell
- JS
- JS
- Java
- Java
- Java
- Java
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Python
- Ruby
- Rust
- Scheme
- Smalltalk
- Smalltalk
roundRobin(items1, items2).each!writeln;
for i := range min(len(items1), len(items2)) {
fmt.Println(items1[i])
fmt.Println(items2[i])
}
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])
}
}
interweave :: [a] -> [a] -> [a] -- optional signature
interweave [] ys = ys
interweave xs [] = xs
interweave (x:xs) (y:ys) = x : y : interweave xs ys
main = mapM_ print $ interweave items1 items2
$ is the function application operator of Haskell to eliminate annoying parentheses.
E.g., f $ g x y z == f (g x y z)
E.g., f $ g x y z == f (g x y z)
const iterator1 = items1[Symbol.iterator]()
const iterator2 = items2[Symbol.iterator]()
let result1 = iterator1.next()
let result2 = iterator2.next()
while(!(result1.done && result2.done)) {
if (!result1.done) {
console.log(result1.value)
result1 = iterator1.next()
}
if (!result2.done) {
console.log(result2.value)
result2 = iterator2.next()
}
}
Approach that purely uses Iterators, similar to the Java Iterator example
IntStream.range(0, Math.max(items1.size(), items2.size()))
.boxed()
.flatMap(idx -> Stream.of(
items1.size() > idx ? items1.get(idx) : null,
items2.size() > idx ? items2.get(idx) : null
))
.filter(Objects::nonNull)
.forEach(System.out::println);
Get a stream of the list indices.
For each index, map to the elements from the lists at that index
Filter out null values which occur when the index is after the end of the list
Print the result
For each index, map to the elements from the lists at that index
Filter out null values which occur when the index is after the end of the list
Print the result
# php 5.3+
print array_reduce( array_map( null, $items1, $items2 ), function($c,$d){return $c.($c?"\n":'').implode("\n", $d); }, "")."\n" ;
#php 5.6+
print implode("\n", array_merge( ...array_map( null, $items1, $items2 )))."\n";
array_map will take one or more arrays, and will supply empty elements for shorter arrays
print for zip @items1, @items2;
print will warn about uninitialized value when lists have different size.
Wrap the statement in a block { } and include
no warnings "uninitialized";
to get rid of the warnings.
Wrap the statement in a block { } and include
no warnings "uninitialized";
to get rid of the warnings.
print(*zip_longest(items1, items2))
"... If the iterables are of uneven length, missing values are filled-in with fillvalue. If not specified, fillvalue defaults to None."
items1.zip(items2){|pair| puts pair}
There will be as much pairs as there are items in items1. items2 will be "padded" with nils or trimmed as needed.