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)
por_plot.ylabel('VOIP')
por_plot.xlabel('Porosity')
por_plot.show()

#plot the heigth graph
hth_plot = plt.plot(listx2,list2)
hth_plot.ylabel('VOIP')
hth_plot.xlabel('Height')
hth_plot.show()

#plot the saturation graph
sat_plot = plt.plot(listx3,list3)
sat_plot.ylabel('VOIP')
sat_plot.xlabel('Saturation')
sat_plot.show()

So I ran the code with the two following lines:

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)
graph_maker(list_maker)

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)
    graph_maker(result)
    

    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)
    graph_maker(*result)
    

    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:

    graph_maker(list_maker)
    graph_maker(*list_maker(vars))
    

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

    x=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)
    graph_maker(*x)