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¶
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:
Example without a key¶
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, an
element that is unknown is left out, 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.