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

二次信任 提交于 2019-12-18 11:04:52

问题


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.


回答1:


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()



回答2:


You can return as many value as you want by separating the values by commas:

def return_values():
    # your code
    return value1, value2

You can even wrap them in parenthesis as follows:

return (value1, value2)

In order to call the function you can use one of the following alternatives:

value1, value2 = return_values() #in the case where you return 2 values

values= return_values() # in the case values will contain a tuple


来源:https://stackoverflow.com/questions/11690333/is-it-possible-to-return-two-lists-from-a-function-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!