Is there any way to map list items to a function along with arguments?
I have a list:
pages = [p1, p2, p3, p4, p5...]
And I have to call function myFunc
corresponding to each list elements along with additional arguments such that the following can be computed
myFunc(p1, additionalArgument) myFunc(p2, additionalArgument)
and so on...
Is there any elegant method for doing this?
Passing Multiple Arguments to map() function Suppose we pass n iterable to map(), then the given function should have n number of arguments. These iterable arguments must be applied on given function in parallel. In multiple iterable arguments, when shortest iterable is drained, the map iterator will stop.
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
The map function has two arguments (1) a function, and (2) an iterable. Applies the function to each element of the iterable and returns a map object.
In Python, you can use map() to apply built-in functions, lambda expressions ( lambda ), functions defined with def , etc., to all items of iterables such as lists and tuples. This article describes the following contents. Note that map() can be substituted by list comprehensions or generator expressions.
You can also use a lambda function:
map(lambda p: myFunc(p, additionalArgument), pages)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With