Is it possible to return two lists from a function in python

heretolearn picture heretolearn · Jul 27, 2012 · Viewed 63.9k times · Source

I am new to python programming and need your help for the following:

I want to return two lists from a function in python. How can i do that. And how to read them in the main program. Examples and illustrations would be very helpful.

Thanks in advance.

Answer

Sven Marnach picture Sven Marnach · Jul 27, 2012

You can return a tuple of lists, an use sequence unpacking to assign them to two different names when calling the function:

def f():
    return [1, 2, 3], ["a", "b", "c"]

list1, list2 = f()