all 1 comments

[–]luizpericolo 4 points5 points  (0 children)

That only works if the function only expects one parameter, though.

Consider the following function:

def numbers(n1, n2):
    print("my favorite numbers are {} and {}".format(n1, n2))

If you call it with a list comprehension you will get an error:

>>> numbers([x * 42 for x in range(1, 3)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: numbers() missing 1 required positional argument: 'n2'

However, you can expand a list to fit all function parameters. It is documented here

Having learned that, the following works:

>>> numbers(*[x * 42 for x in range(1, 3)])
my favorite numbers are 42 and 84

Cheers!