is it possible to use the variable list from a function?

here is an example?
is it possible to call alist with its last used content?

def example(a):
    alist=[]
    alist.append(a)
    print alist

example(1)
print alist

thanks

The variables in a function's body are local to this function and can't be used outside the function. The solution is to return the variable and to catch the return value when the function is called

def example(a):
    alist=[]
    alist.append(a)
    print alist
    return alist # <---- return alist to the calling environment

thelist = example(1) # <----- we catch the returned list
print thelist

Note that we could have used the same name alist instead of thelist. I changed the
name to show that we are dealing with 2 different variables.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.