TransWikia.com

Accessing item in list of lists using Google Earth Engine

Geographic Information Systems Asked by Natalia Quintero on October 14, 2020

How do I access an item in a list of lists in GEE?

I know that using the method get() returns the element at the specified position in list. But the result of list.get(0) is [1, 2, 3], and I want the first element of each list in the list.

var list = ee.List([
  [1, 2, 3],
  [2, 4, 5],
  [3, 4, 5]
]);

One Answer

Usually when you want to do something with each item in a list, you use map. In this case since you want the first element, you put .get(0) inside the map function.

var list = ee.List([[1, 2, 3], [10, 20, 30], [100, 200, 300]]);

print(list.map(function (innerList) {
  return ee.List(innerList).get(0);
}));

// Prints: [1,10,100]

This example contains ee.List(innerList).get(0) instead of innerList.get(0) because the map doesn't know that every element of list is a list — this cast is necessary in most cases when you map over a list to specify the expected type so you can call .get or other specific method on it.

Correct answer by Kevin Reid on October 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP