简体   繁体   中英

How do I call a list from another function in Python?

I have a list in a function that I have appended values into, now I am trying to wonder how I can call that list in another function.

I don't know if I completely understand you but you can return that list and use it into another function.

def function():
    x=[]
    for i in range(5):
        x.append(i)
return x

def print_function():
    x=function()
    print(x)
return

You can declare a new global variable to point your list inside the function.

pointedList = []
copiedList = []

def function():
    x = []
    x.append("Something")

    pointedList = x #Changes made to pointedList will change values in x
    copiedList = x.copy() #Changes made to copiedList will not reflect in x

    '''
    Rest of the program
    '''

def newFunction():
    '''
    You can use pointedList and copiedList here
    '''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM