0

I'm new to python and I was wondering if there's a way for me to access a list or dictionary with data already stored in it from a different function.

I was wondering if there's a way for me to make the list global so I can access it outside the function, or any other alternative solution.

def upload():
    numlist = [1,2,3,4]

def add():
   for i in numlist:
       print(i)
Amit Yadav
  • 4,422
  • 5
  • 34
  • 79
to3
  • 29
  • 5
  • Is `numlist` only accessible to the function `upload`? – pseudomonas Oct 28 '19 at 17:26
  • You can't. That's why it's called a *local variable*. – wim Oct 28 '19 at 17:26
  • This case you should be returning the `numlist` variable and then using it. But yes, python has a [`global`](https://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python) keyword. This would be quite abusive though – modesitt Oct 28 '19 at 17:26
  • That is not possible. There are several other options which would work, but which exactly is the best depends on what you are actually trying to do: numlist can be returned from upload, our it can be passed to upload, or it could be a member variable of a class... – zvone Oct 28 '19 at 17:28
  • 3
    I suggest that you work through a tutorial on functions and their scopes (variable visibility). What you've asked to do, if done directly, is poor program design. Instead, you should have `upload` share that data to a calling program, that then shares it with `add`. This is done through the function parameter list (input) and `return` value (output). – Prune Oct 28 '19 at 17:29
  • yea i figured.. I'm trying to make webpage that takes an excel file.. take the data from that excel file and process the data in a different function .. I was wondering if there's a way for me to access data in a different function – to3 Oct 28 '19 at 17:30

4 Answers4

4

Generally speaking global variables are not brilliant, so it would be better to have a function that creates and returns the list, and then use that in the second function. Even better, you could have a 3rd function that coordinates the 2 separate parts - creation of the list, and processing of the list:

def create_numlist(): 
    numlist = [1,2,3,4]
    return numlist

def print_numbers(numbers):
    for i in numbers:
        print(i)

def main():
    numbers = create_numlist()
    print_numbers(numbers)

Now the function that loads/creates the list is completely separate from the function that does something with that list.

wim
  • 338,267
  • 99
  • 616
  • 750
Tom Dalton
  • 6,122
  • 24
  • 35
1

One option is to pass it as parameter, hooking everything together

def add(add_list):
  for value in add_list:
    print(value)

def upload():
  numlist = [1, 2, 3, 4]
  add(numlist)
H_DANILO
  • 321
  • 1
  • 9
0

No, you can't access local variables outside of their functions.

An option would be to define a Class that contains:

  1. The variables you're using
  2. The functions that need access to these variables.

This would allow you to define local variables that can be accessed by all of the functions in the class without having to return values from one function to the next.

Example :

class Upload:
    def __init__(self):
        self.numlist = [1,2,3,4]

    def add(self):
        for i in self.numlist:
            print(i)

foo = Upload()
foo.add()

Output :

1
2
3
4
Hunted
  • 88
  • 1
  • 7
-1

Yes, as you guessed you can make the list global.

You just need to declare it outside of the function (otherwise it is only local to the function).

numlist = None

def upload():
    global numlist
    numlist = [1,2,3,4]

def add():
    for i in numlist:
        print(i)

upload()
add()

Global variables are somewhat frowned upon. You might research this topic in some depth, for example Why are global variables evil?

Therefore it would be better to try and structure your code so that a global variable is not required. For example:

def upload():
    return [1,2,3,4]

def add():
    numlist = upload()
    for i in numlist:
        print(i)

add()
Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • 2
    I think I'm getting downvoted for using a global variable - I would ask people to please consider that the OP specifically asked for _a way for me to make the list global_ – Anentropic Oct 28 '19 at 17:36
  • I expect better from a 22k rep user than to lead a beginner down the garden path like this. Instead of showing how to give global mutable state to a function, which is a poor approach, you have to see the "question behind the question". After your edits, the answer is now just duplicating content already present in other answers. – wim Oct 28 '19 at 17:38
  • 1
    Read it like this: You can't [*use a local variable outside of the function*]. The solution to that is generally not to make it into a global variable, and the knee-jerk response to post an answer showing how to use the global keyword is not helpful content to have on stack overflow. – wim Oct 28 '19 at 17:45