flat_map_deep()
Recursively flat maps a given collection. This is similar to :func:`flat_map`, but will continue to flatten the mapped results until they are no longer iterable.
Implementation
Args: collection: Collection to iterate over. func: Iteratee applied per iteration. Returns: Fully flattened mapped list.
Example
nested = lambda x: [[x, x], [x, x]]
flat_map_deep([1, 2], nested)
Expected output: [1, 1, 1, 1, 2, 2, 2, 2]
Source Code
def flat_map_deep(collection: List[Any], func: Callable[[Any], List[Any]]) -> List[Any]:
def _deep(arr):
out: List[Any] = []
for y in arr:
if isinstance(y, (list, tuple)):
out.extend(_deep(y))
else:
out.append(y)
return out
return _deep(flat_map(collection, func))