for_each()
Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Each invocation of iteratee is called for its side-effects upon the collection. Collection may be either an object or an array. Iteration is stopped if predicate returns Falsey value. The predicate is bound to thisArg and invoked with three arguments: (value, index|key, collection).
Implementation
Args: collection: The collection to iterate over. func: The function invoked per iteration. Returns: The collection.
Example
for_each([1, 2, 3], lambda x: print(x))
Expected output: 1
2
3
Source Code
def for_each(
collection: Union[Dict[Any, Any], List[Any]], func: Callable[[Any], None]
) -> Union[Dict[Any, Any], List[Any]]:
if isinstance(collection, dict):
for v in collection.values():
func(v) # noqa: E701
else:
for v in collection:
func(v) # noqa: E701
return collection