One option is a list comprehension:
[add(x, 2) for x in [1, 2, 3]]
More options:
a = [1, 2, 3]
import functools
map(functools.partial(add, y=2), a)
import itertools
map(add, a, itertools.repeat(2, len(a)))
Answer from Sven Marnach on Stack OverflowDifferences between lambda and .map()
python - How to do multiple arguments to map function where one remains the same - Stack Overflow
python - Send two argument using map and lambda functions and return two values - Stack Overflow
Unleashing the Power of Lambda Functions in Python: Map, Filter, Reduce
Videos
I'm looking to expand my knowledge on the subject these upcoming days and was hoping someone could shed some clarity. Lambda escapes me sometimes because I THINK I understand, but then when I look at it in practice I get the cold fingers.
What I was wondering is if there was any similarity between the two? They look to me that they accomplish similar tasks. I'm not sure if they're the same thing or can be made into the same thing or if they're different? I was also looking at both of them and wondering if they're pretty much synonymous with a "for" loop?
One option is a list comprehension:
[add(x, 2) for x in [1, 2, 3]]
More options:
a = [1, 2, 3]
import functools
map(functools.partial(add, y=2), a)
import itertools
map(add, a, itertools.repeat(2, len(a)))
The docs explicitly suggest this is the main use for itertools.repeat:
Make an iterator that returns object over and over again. Runs indefinitely unless the times argument is specified. Used as argument to
map()for invariant parameters to the called function. Also used withzip()to create an invariant part of a tuple record.
And there's no reason for pass len([1,2,3]) as the times argument; map stops as soon as the first iterable is consumed, so an infinite iterable is perfectly fine:
>>> from operator import add
>>> from itertools import repeat
>>> list(map(add, [1,2,3], repeat(4)))
[5, 6, 7]
In fact, this is equivalent to the example for repeat in the docs:
>>> list(map(pow, range(10), repeat(2)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
This makes for a nice lazy-functional-language-y solution that's also perfectly readable in Python-iterator terms.
Since you say you only want a single model, not a new model for each value, this is fairly simple. Change:
hnd = map(lambda (valua): function_f(valua), list_value)
to:
model = ... initialize a model that will be passed to every call ...
hnd = map(lambda valua: function_f(valua, model), list_value)
Just make sure function_f returns both the new value and model, e.g. if it previously did:
def function_f(val, model):
... calculate newval and make newmodel ...
return newval
just change it to:
def function_f(val, model):
... calculate newval and make newmodel ...
return newval, newmodel
Note: If need to use lambdas to use map, don't use map; it gains you nothing (a generator expression or list comprehension is going to run with the same speed or even faster in most cases where the mapping function isn't a CPython built-in). That said, in your particular case you don't need a lambda, for example, you can do:
from future_builtins import map # Only on Py2; Py3 map is good
from itertools import repeat
model = ... initialize a model that will be passed to every call ...
hnd = map(function_f, list_value, repeat(model))
or just use a generator expression (unless the function is a Python built-in implemented in C, map basically never gains you performance; if you don't want to think about whether map is appropriate, always using list comprehensions/generator expresssions instead of map is a good idea):
# Change outside parens to brackets, [], for list comp
hnd = (function_f(x, model) for x in list_value)
By using zip, you can use map with such function. I am providing a sample code as you have asked to demonstrate
ls1 = [1,2,3,4,5]
ls2 = [1,2,3,4,5]
>>> def func(x,y):
return x+1,y+1
>>> map(lambda (v1, v2): func(v1, v2), zip(ls1, ls2))
[(2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]
Hello Pythonistas!
I've been on a Python journey recently, and I've found myself fascinated by the power and flexibility of Lambda functions. These anonymous functions have not only made my code more efficient and concise, but they've also opened up a new way of thinking about data manipulation when used with Python's built-in functions like Map, Filter, and Reduce.
Lambda functions are incredibly versatile. They can take any number of arguments, but can only have one expression. This makes them perfect for small, one-time-use functions that you don't want to give a name.
Here's a simple example of a Lambda function that squares a number:
square = lambda x: x ** 2
print(square(5)) # Output: 25
But the real power of Lambda functions comes when you use them with functions like Map, Filter, and Reduce. For instance, you can use a Lambda function with `map()` to square all numbers in a list:
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # Output: [1, 4, 9, 16, 25]
You can also use a Lambda function with `filter()` to get all the even numbers from a list:
numbers = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even) # Output: [2, 4]
And finally, you can use a Lambda function with `reduce()` to get the product of all numbers in a list:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 120
Understanding and using Lambda functions, especially in conjunction with Map, Filter, and Reduce, has significantly improved my data manipulation skills in Python. If you haven't explored Lambda functions yet, I highly recommend giving them a try!
Happy coding!