for_each_right()
Iterates over elements of a collection from right to left and invokes a function for each element. The function is invoked with one argument: the current element.
Implementation
Args: collection: The collection to iterate over. Can be a dictionary or a list. func: The function to invoke for each element. It should accept a single argument. Returns: The original collection.
Example
for_each_right([1, 2, 3], lambda x: print(x))
Expected output: 3
2
1
Source Code
def for_each_right(
collection: Union[Dict[Any, Any], List[Any]], func: Callable[[Any], None]
) -> Union[Dict[Any, Any], List[Any]]:
items = (
list(collection.values()) if isinstance(collection, dict) else list(collection)
)
for v in reversed(items):
func(v) # noqa: E701
return collection