I want to randomly select a feature from each group in a FeatureCollection. My actual data has many groups, so filtering each group and choosing one from each group is not an option. I tried using reduceColumns() with a grouped reducer to choose a random item from each group. My idea was to extract the random values from that dictionary as a list, and then use that to filter the original feature collection, but got an error when trying to convert the dictionary to a list.

How can I get a list of the random values from my dictionary (see below)?

var fc = ee.FeatureCollection([
  ee.Feature(null, {group: 'A'}),
  ee.Feature(null, {group: 'A'}),
  ee.Feature(null, {group: 'B'}),
  ee.Feature(null, {group: 'B'}),
  ee.Feature(null, {group: 'C'}),
  ee.Feature(null, {group: 'C'})

// Add random column.
var fc = fc


// Get a random value for each group
//  (the random value is nested deep in a dictionary)
var randomDict = fc
    selectors: ['random', 'group'],
    reducer: ee.Reducer.min().group({
      groupField: 1

// Extract the random value from dictionary, return list
var randomList = randomDict
    return e.get('min')
print(randomList) // randomDict.get(...).map is not a function

// Use list of random values to filter the original fc
var randomFC = fc
  .filter(ee.Filter.inList('random', randomList))


1 Answer 1


You will need to correctly cast the objects to an ee.List() and ee.Dictionary():

// Extract the random value from dictionary, return list
var randomList = ee.List(randomDict
    return ee.Dictionary(e).get('min')

link code

Your Answer

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

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