How to use a function that returns lists as input to another function?

I first wrote a function that took 18 arguments and turned them into 6 different lists. Here is the code:

def list_maker(val1,val2,val3,val4,val5,val6,val7,val8,val9,por1,por2,por3,hth1,hth2,hth3,sat1,sat2,sat3):

#Make the voip list
list1 = [val1,val2,val3]
list2 = [val4,val5,val6]
list3 = [val7,val8,val9]

#Make the variable list
list_por = [por1,por2,por3]
list_hth = [hth1,hth2,hth3]
list_sat = [sat1,sat2,sat3]

return list1,list2,list3,list_por,list_hth,list_sat

That part worked just fine (I'll make it look better once it actually works). Now, my idea was to use that function as input to this other function right below to create plots:

def graph_maker(listx1,listx2,listx3,list1,list2,list3):

#plot the saturation graph
por_plot = plt.plot(listx1,list1)

#plot the heigth graph
hth_plot = plt.plot(listx2,list2)

#plot the saturation graph
sat_plot = plt.plot(listx3,list3)

So I ran the code with the two following lines:


And the error I'm getting is:

graph_maker() missing 5 required positional arguments: 'listx2', 'listx3', 'list1', 'list2', and 'list3'

What I understand from it, it looks as if list_maker() is actually returning only one list, and obviously the graph_maker function needs 6 arguments. Any ideas?

Thanks for the help!

2 answers

  • answered 2018-10-11 19:37 LuRsT

    Marco, when you pass list_maker into graph_maker, you're not actually passing the result of the function (the lists you want) as input to graph_maker, you're actually passing the function into it.

    But it's not just a matter of doing this:

    result = list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)

    Because the function list_maker returns a tuple with all the lists, you need to expand them this way:

    result = list_maker(voip1,voip2,voip3,voip4,voip5,voip6,voip7,voip8,voip9,0.3,0.2,0.15,100,150,200,0.8,0.6,0.5)

    The asterisk will expand the tuple into the 5 arguments that the function requires, does this make sense?

  • answered 2018-10-11 19:37 G. Anderson

    You're missing part of the inner function call in the outer function call:


    Or assign your initial function call to a variable and use * to unpack the values (credit to @zondo)