Home > front end >  How to pass different variables to multiple functons?
How to pass different variables to multiple functons?

Time:02-13

I am very new to python and I have three lists that I edited in a function and want to pass these three lists to multiple different functions, This is basic example of what I mean:

def editing():
    firstlist = [1,2,3]
    seclist = [4,5,6]
    thirdlist = [7,8,9]

def aa():


def bb():

I would like to send firstlist to aa() and all three lists to bb() but I am confused. What would be the most efficient way to do that?

CodePudding user response:

You could work with global variables, but that is not good practice. Instead, return the lists from your function, as a tuple, and pass them as arguments into the other functions.

def editing():
    firstlist = [1,2,3]
    seclist = [4,5,6]
    thirdlist = [7,8,9]

    return firstlist, seclist, thirdlist


def aa(firstlist):
    pass


def bb(firstlist, seclist, thirdlist):
    pass


one, two, three = editing()
aa(one)
bb(one, two, three)

CodePudding user response:

You can return in the first function a tuple of the list:

def editing():
    firstlist = [1,2,3]
    seclist = [4,5,6]
    thirdlist = [7,8,9]
    return firstlist, seclist, thirdlist

So, you just call this function and get the index of the tuple you want:

def aa():
    return editing()[0]
print(aa())

Output:

[1, 2, 3]

And so on

  • Related