mhwh
mhwh

Reputation: 490

simultaniusly append two lists with one function output

I would like to append two list with one output:

def func(x) : return "foo" + str(x), "bar"+ str(x)
   

foo_list = []
bar_list = []

for i in range(3):
    foo, bar = func(i)
    foo_list.append(foo)
    bar_list.append(bar)

is it possible to do this without temporary vaiables?

Upvotes: 0

Views: 59

Answers (2)

Vitus
Vitus

Reputation: 46

It is certainly possible, but it isn't simpler to understand. So don't do it, but this would be the way:

for i in range(3):
    any(map(list.append, (foo_list, bar_list), func(i)))

Upvotes: 3

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

You can do this but it will make the function call two times instead of one.

def func(x) : return "foo" + str(x), "bar"+ str(x)
   
foo_list = []
bar_list = []

for i in range(3):
    foo_list.append(func(i)[0])
    bar_list.append(func(i)[1])

Upvotes: 1

Related Questions