1

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.

3
  • 2
    Can you show us what you have tried and what doesn't work for you? Commented May 24, 2020 at 3:57
  • call is something you do to functions to execute them. It's not clear what you mean in the context of a list. Are you just trying to access the list? Commented May 24, 2020 at 3:59
  • @MarkMeyer Hello, yes, I have created a list in one function, and I would like to be able to access that list in another function. Commented May 24, 2020 at 23:13

2 Answers 2

1

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
    '''
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.