Iterate over two lists (of differing length) backwards in Scala

list, loops, scala

Solution

A simple way is :

 List('a','b','c').reverse zip List(1,2).reverse

Reversing the list is `O(n)` however, if you're worried about efficiency.

According to `List`'s scaladoc, using `reverseIterator` might be more efficient. That way you don't creat a new list like with `reverse`, but traverse it as you keep iterating. That'd be :

val it = list1.reverseIterator zip list2.reverseIterator  //returns an Iterator you can force
it.toList // List((c,2), (b,1))

Problem

What is the most efficient way to iterate over two lists (of differing length) backwards in Scala. So for two lists ``` List(a,b,c) and List(1,2) ``` the pairs would be ``` (c,2) and (b,1) ``` Note: I would rather not do a reverse of each list.

Original source

Related problems