Skip to content

FLATTEN

Description

FLATTEN extracts values from a list. Given a list of dictionaries and a key (or a tuple of keys), it returns a flat list of the values at that key across all dictionaries.

If called with no second argument, it removes one level of nesting and returns a list.

Example with a single key

FLATTEN([{"key": "a", "name": "Alice"}, {"key": "b", "name": "Bob"}], "key")

This extracts the value at "key" from each dictionary, returning ["a", "b"].

Example with multiple keys

FLATTEN([{"key": "a", "age": 25, "name": "Alice"}, {"key": "b", "age": 30, "name": "Bob"}], ("key", "name"))

This returns only the named keys from each dictionary:

[{"key": "a", "name": "Alice"}, {"key": "b", "name": "Bob"}]

Example without a key

FLATTEN([["a", "b"], ["c", "d"]])

This concatenates the sublists into a single list, returning ["a", "b", "c", "d"].

An element that is not itself a list is kept as one element, so a list that is already flat comes back unchanged and an empty list returns an empty list. JOIN, SUM and the other operators that read a list of values flatten what you give them anyway, so you only need FLATTEN where the shape of the list itself matters, such as the rows of a markdown_table.