Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function multiple times consecutively with different arguments in Python (3.x)?

I have a piece of code that looks like this:

myfunction(a, b, c)
myfunction(d, e, f)
myfunction(g, h, i)
myfunction(j, k, l)

The number of arguments do not change, but the function has to be called consecutively with different values each time. These values are not automatically generated and are manually inputted. Is there an inline solution to do this without creating a function to call this function? Something like:

myfunction(a, b, c)(d, e f)(g, h, i)(j, k, l)

Any help appreciated. Thanks in advance!

like image 374
Terrornado Avatar asked Oct 14 '25 18:10

Terrornado


2 Answers

Simple, use tuple unpacking

tripples = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]
for tripple in tripples:
    print(myfunction(*tripple))
like image 166
Xero Smith Avatar answered Oct 17 '25 06:10

Xero Smith


I'm surprised that nobody mentioned map

map(func, iter)

It maps each iterable in iter to the func passed in the first argument.

For your use case it should look like

map(myfunction, *zip((a, b, c), (d, e, f), (g, h, i), (j, k, l)))
like image 24
vpshastry Avatar answered Oct 17 '25 07:10

vpshastry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!