UniCoreFW

flat_map()

Creates a flattened list of values by running each element in collection through `func` and flattening the mapped results.

Implementation

Args: collection: Collection to iterate over. func: Iteratee applied per iteration. Returns: Flattened mapped list.

Example

duplicate = lambda n: [[n, n]] flat_map([1, 2], duplicate)

Expected output: [[1, 1], [2, 2]]

Source Code

def flat_map(collection: List[Any], func: Callable[[Any], List[Any]]) -> List[Any]: result: List[Any] = [] for x in collection: res = func(x) if isinstance(res, (list, tuple)): result.extend(res) return result