0

Sometimes, I have need to write a function that returns a few variables/lists. It might look like this

def returnLists():
    list1 = []
    list2 = []
    var1 = None

    # do stuff and add values to list or update var1

    return list1, list2, var1

Is there a smoother way to do that? What if instead of 2 lists and 1 variable I have 12 variables I'd like to return, or 8 lists, etc.

The best I have so far is

var = val if 'var' not in locals() else var=var+val
if 'list1' in locals() list1.append(val1) else list=[val1]

Is there a better way to do this in python that doesn't take so many lines? To be clear, it's ok to me that it returns more than 1 variable, I just want to be able to do it where it doesn't take at least 2*variables lines (1 for initialization and 1 for adding/appending the variable)

In my particular case the actual example was I had a pandas dataframe where one column was a baseball stat line AB-H-2B-3B-HR-K-BB (e.g. '33-7-2-0-1-2-4'). I wanted to sum up all the stats. I broke the string into a list and then wanted to sum the list. So, I opened with

AB=0
H=0
DB=0 #etc
...
BB=0
for game in list:
  boxList=game.box.split('-')
  ab+=boxList[0]
  h+=boxList[1] #etc
  ...
  bb+=boxList[6]
return ab,h,2b,3b,hr,k,bb
Sparkles
  • 43
  • 6
  • what dictates how many lists/variables you use in the function? – Mitchnoff Aug 29 '22 at 16:18
  • 2
    I think a "nice" way to do this is creating a class and doing all your variables its members. – Ivan Aug 29 '22 at 16:26
  • 1
    Whenever you find yourself naming variables `xx1, xx2, ...` you should be using a list, not separate variables. – Barmar Aug 29 '22 at 16:26
  • 1
    Does this answer your question? [Alternatives for returning multiple values from a Python function](https://stackoverflow.com/questions/354883/alternatives-for-returning-multiple-values-from-a-python-function) – mkrieger1 Aug 29 '22 at 16:27
  • 2
    Implementing some ugly code inside your function just to solve "I don't want to initialize a bunch of variables because it makes my code longer or it's too much work" doesn't feel like the right problem to solve. For a function to return more than a few variables, I can't help but think a data structure like a dictionary would be way more appropriate and would mean only initializing a single object. If your function is significantly complex then a class/OOP would be a more appropriate route. – JNevill Aug 29 '22 at 16:27
  • Just whatever I need for the function, really. In this particular case I had a pandas DF and one of the columns was a '-' delimited baseball stat line. Something like 'AB-H-2B-3B-HR-K-BB'. So, for that one, I wanted to initialize and eventually return 8 variables that summed the different stats. But it's come up in other code. – Sparkles Aug 29 '22 at 16:27
  • Can you please provide an actual example? There are *many* ways to "that ugly thing nicer" and often enough the nicest way is "don't do that ugly thing" - but that depends on what that ugly thing actually does. For example, if you want to "return 8 variables that summed the different stats" then the common approach is to return *one* dict of arbitrary many key-stats pairs. – MisterMiyagi Aug 29 '22 at 16:58
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – MisterMiyagi Aug 29 '22 at 17:03
  • 1
    The way you are doing it right now is the preferred way if you are just going to use local variables. – juanpa.arrivillaga Aug 29 '22 at 17:14
  • Why not just sum the desired columns in the dataframe? I'm confused as to what you are trying to achieve. can you give me a sample of the data set, and what your expected outcome should be? – chitown88 Aug 30 '22 at 08:30

0 Answers0