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