Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map list item to function with arguments

Tags:

python

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?

like image 919
Sushant Gupta Avatar asked Apr 18 '12 15:04

Sushant Gupta


People also ask

How do you pass multiple parameters to a map in Python?

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.

Can we pass a list an argument in a function?

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.

How many arguments can be passed to map () 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.

Can I map a list in Python?

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.


1 Answers

You can also use a lambda function:

map(lambda p: myFunc(p, additionalArgument), pages) 
like image 187
amarchiori Avatar answered Sep 19 '22 10:09

amarchiori