Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, return multiple arrays in new lines

Tags:

python

I have 3 arrays, a1, a2, a3

I would like to return all three of them at once.

I have

return a1, a2, a3 but it returns them all on the same line, I was wondering how I would return them each on a new line

like image 890
user1730056 Avatar asked Dec 15 '22 17:12

user1730056


1 Answers

Do you mean like this?

>>> def f():
...   a1 = [1, 2, 3]
...   a2 = [4, 5, 6]
...   a3 = [7, 8, 9]
...   return a1, a2, a3
... 
>>> f()
([1, 2, 3], [4, 5, 6], [7, 8, 9])

You can unpack the return value like this

>>> b1, b2, b3 = f()
>>> b1
[1, 2, 3]
>>> b2
[4, 5, 6]
>>> b3
[7, 8, 9]

Or print it on 3 lines like this

>>> print "\n".join(str(x) for x in f())
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
like image 76
John La Rooy Avatar answered Jan 07 '23 04:01

John La Rooy