Ahmad
Ahmad

Reputation: 23

How to pass different variables to multiple functons?

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?

Upvotes: 1

Views: 43

Answers (2)

The Fool
The Fool

Reputation: 20547

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)

Upvotes: 3

Shrmn
Shrmn

Reputation: 398

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

Upvotes: 3

Related Questions