4

In Google Earth Engine, I have two lists:

var list1 = ee.List.sequence(50, 100, 50);
var list2 = ee.List.sequence(2,3,1);

How to make a new list containing every possible pair from list1 to list2 ?

I tried zip() function which gave results as follows: [ [ 50, 2 ], [ 100, 3 ] ].

Pairs [50,3] and [100,2] are missing from this new list. Is there any way out?

1 Answer 1

8

You could do something like this:

var list1 = ee.List.sequence(50, 100, 50);
var list2 = ee.List.sequence(2,3,1);

var combinations = ee.List(
  list1.iterate(function (e1, acc) {
    var pairs = list2.map(function (e2) {
      return [e1, e2]
    })
    return ee.List(acc).cat(pairs)
  }, ee.List([]))
)
print(combinations)

https://code.earthengine.google.com/9b9ad524cd9a16b1f7dcc2720098aba1

3
  • That solves a problem I am facing... but I would like to understand what the acc stands for? Dec 1, 2022 at 10:23
  • 1
    That's the accumulator. Starting out as an empty list, then you update it for every iteration Dec 1, 2022 at 10:43
  • Thank you very much! Now it's much more clear Dec 5, 2022 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.