7

Say we have a function func and in that function there's a list of n items. The n number changes depending on the argument(s) given to func. There's also a lambda function in func (created with eval any time func is called) that takes n number of arguments and returns something. Is there any way you could pass the items from the list to the lambda function (items from the list and arguments in lambda are always equal to each other in number, the only difference is that list items are wrapped in [] and arguments are not)? You can't simply insert the list into lambda since it sees it as one argument when in fact the list might have 3, 30 or any number of arguments. A simplified picture:

def func(*args):
    lst = [n items where n changes depending on *args]
    lambda n arguments/items: do sth
2
  • 1
    This is looking a bit like an XY Problem Commented Dec 10, 2014 at 22:21
  • why are you using a lambda in the first place, what are you trying to do? Commented Dec 10, 2014 at 22:21

1 Answer 1

7

You can do something similar to *arg and **kwargs with lambda:

>>> a = lambda *args, **kwargs: (args, kwargs)
>>> a(1, 2, a="a", b="b")
((1, 2), {'a': 'a', 'b': 'b'})
Sign up to request clarification or add additional context in comments.

3 Comments

But my lambda function is already created and it looks for example like this: lambda x,y,z: z*100 + x - y. So is my list and it looks like this: lst = [4,5,6]. The question is how to pass SPECIFIC items from the list into lambda function (knowing only that the number of items and the number of arguments is the same). If there's no way of doing it.. fine. I'd just like to know that for sure :)
is my_lambda_func(*lst) what you are looking for?
cool, I thought that '*' symbol only allows for many arguments to be passed to a function, but it can actually 'unpack' a list and serve its items as arguments. Thx! It worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.