How should I return rho, c, k, d and layers [...] ?

Simply do it:

return rho, c, k, d, layers

And then you'd call it like

rho, c, k, d, layers = material()
print d[1]

Note that the more stuff you're returning, the more likely it is you're going to want to wrap it all together into some structure like a dict (or namedtuple, or class, etc.)

Answer from DSM on Stack Overflow
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Return Multiple Values from a Function in Python | note.nkmk.me
April 23, 2025 - You can unpack multiple return values and assign them to separate variables. ... The same applies to three or more return values. def test2(): return 'abc', 100, [0, 1, 2] a, b, c = test2() print(a) # abc print(b) # 100 print(c) # [0, 1, 2] ...
🌐
Finxter
blog.finxter.com › python-return-two-or-multiple-lists-dicts-arrays-dataframes-from-a-function
Python Return Two or Multiple Lists, Dicts, Arrays, DataFrames From a Function – Be on the Right Side of Change
To unpack a tuple of multiple returned values in Python, just assign the return value to the same number of variables. For example: def my_function(): a = 1 b = 2 c = 3 return a, b, c x, y, z = my_function() Yes, you can use type hints with the typing.Tuple module to specify the types of multiple ...
🌐
freeCodeCamp
freecodecamp.org › news › python-returns-multiple-values-how-to-return-a-tuple-list-dictionary
Python Return Multiple Values – How to Return a Tuple, List, or Dictionary
July 20, 2020 - By Amy Haddad You can return multiple values from a function in Python. To do so, return a data structure that contains multiple values, like a list containing the number of miles to run each week. def miles_to_run(minimum_miles): week_1 = minimum...
🌐
GeeksforGeeks
geeksforgeeks.org › python › g-fact-41-multiple-return-values-in-python
Returning Multiple Values in Python - GeeksforGeeks
List is an ordered collection of items, created using square brackets []. A list can be used to return multiple values from a function, especially when the number of values is fixed.
Published   July 1, 2025
🌐
Drbeane
drbeane.github.io › python › pages › functions › returning_multiple.html
Returning Multiple Values — Python for Data Science
It is possible for a Python function to return multiple values. To To accomplish this, we can combine the desired return values together into a list or a tuple, which we then return.
🌐
Quora
quora.com › I-have-written-a-function-in-Python-that-returns-multiple-values-one-after-another-How-do-I-store-these-values-in-a-list
I have written a function in Python that returns multiple values one after another. How do I store these values in a list? - Quora
Answer (1 of 5): you can either modify your function to return them as a list [code]def f(): return [1, 2, 3][/code] or convert the returned output to a list [code]def f(): return 1, 2, 3 result = list(f())[/code] if your function is a generator [code] def f(): for i in [1,2,3]: y...
🌐
Python Land
python.land › home › tips & tricks › python return multiple values
Python Return Multiple Values • Python Land Tips & Tricks
May 16, 2023 - All you need to do is list your values after the return statement, separated by commas. Here’s a runnable example of how to return multiple values and how to assign them to multiple variables at once:
Find elsewhere
🌐
datagy
datagy.io › home › python posts › python: return multiple values from a function
Python: Return Multiple Values from a Function • datagy
December 19, 2022 - Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function. Similar to returning multiple values using tuples, as shown in the previous examples, we can return multiple values from a Python function using lists.
🌐
Stack Overflow
stackoverflow.com › questions › 58048148 › how-can-i-return-multiple-lists-from-a-function-retaining-their-names
python - How can i return multiple lists from a function, retaining their names? - Stack Overflow
Another way is to return a dict : return { 'my_list_1': my_list_1, 'my_list_2': my_list_2 } then, you are able to do the following snippet (but it is useful when the keys are very heterogeneous and not an enumeration as my_list_N, index would ...
Top answer
1 of 3
97
>>> rr,tt = zip(*[(i*10, i*12) for i in xrange(4)])
>>> rr
(0, 10, 20, 30)
>>> tt
(0, 12, 24, 36)
2 of 3
30

Creating two comprehensions list is better (at least for long lists). Be aware that, the best voted answer is slower can be even slower than traditional for loops. List comprehensions are faster and clearer.

python -m timeit -n 100 -s 'rr=[];tt = [];' 'for i in range(500000): rr.append(i*10);tt.append(i*12)' 
10 loops, best of 3: 123 msec per loop

> python -m timeit -n 100 'rr,tt = zip(*[(i*10, i*12) for i in range(500000)])' 
10 loops, best of 3: 170 msec per loop

> python -m timeit -n 100 'rr = [i*10 for i in range(500000)]; tt = [i*10 for i in range(500000)]' 
10 loops, best of 3: 68.5 msec per loop

It would be nice to see list comprehensionss supporting the creation of multiple lists at a time.

However,

if you can take an advantage of using a traditional loop (to be precise, intermediate calculations), then it is possible that you will be better of with a loop (or an iterator/generator using yield). Here is an example:

$ python3 -m timeit -n 100 -s 'rr=[];tt=[];' "for i in (range(1000) for x in range(10000)): tmp = list(i); rr.append(min(tmp));tt.append(max(tmp))" 
100 loops, best of 3: 314 msec per loop

$ python3 -m timeit -n 100 "rr=[min(list(i)) for i in (range(1000) for x in range(10000))];tt=[max(list(i)) for i in (range(1000) for x in range(10000))]"
100 loops, best of 3: 413 msec per loop

Of course, the comparison in these cases are unfair; in the example, the code and calculations are not equivalent because in the traditional loop a temporary result is stored (see tmp variable). So, the list comprehension is doing much more internal operations (it calculates the tmp variable twice!, yet it is only 25% slower).

🌐
Flexiple
flexiple.com › python › python-return-multiple-values
Return multiple values from a function in Python | Flexiple Tutorials - Flexiple
#Returning Multiple Values using Tuples def multiple(): operation = "Sum" total = 5+10 return operation, total; operation, total = multiple() print(operation, total) #Output = Sum 15 · A common confusion here is that the syntax of tuple requires a pair of brackets (). Although this is true, Python does not always require brackets to identify a tuple. Similar to the previous methods, lists can also be used to return multiple values, too.
🌐
Career Karma
careerkarma.com › blog › python › python: return multiple values from a function
Python: Return Multiple Values from a Function | Career Karma
December 1, 2023 - You can return multiple values by bundling those values into a dictionary, tuple, or a list. These data types let you store multiple similar values. You can extract individual values from them in your main program.
Top answer
1 of 2
19

I think the choices need to be considered strictly from the caller's point of view: what is the consumer most likely to need to do?

And what are the salient features of each collection?

  • The tuple is accessed in order and immutable
  • The list is accessed in order and mutable
  • The dict is accessed by key

The list and tuple are equivalent for access, but the list is mutable. Well, that doesn't matter to me the caller if I'm going to immediately unpack the results:

score, top_player = play_round(players)
# or
idx, record = find_longest(records)

There's no reason here for me to care if it's a list or a tuple, and the tuple is simpler on both sides.

On the other hand, if the returned collection is going to be kept whole and used as a collection:

points = calculate_vertices(shape)
points.append(another_point)
# Make a new shape

then it might make sense for the return to be mutable. Homogeneity is also an important factor here. Say you've written a function to search a sequence for repeated patterns. The information I get back is the index in the sequence of the first instance of the pattern, the number of repeats, and the pattern itself. Those aren't the same kinds of thing. Even though I might keep the pieces together, there's no reason that I would want to mutate the collection. This is not a list.

Now for the dictionary.

the last one creates more readable code because you have named outputs

Yes, having keys for the fields makes heterogenous data more explicit, but it also comes with some encumbrance. Again, for the case of "I'm just going to unpack the stuff", this

round_results = play_round(players)
score, top_player = round_results["score"], round_results["top_player"]

(even if you avoid literal strings for the keys), is unnecessary busywork compared to the tuple version.

The question here is threefold: how complex is the collection, how long is the collection going to be kept together, and are we going to need to use this same kind of collection in a bunch of different places?

I'd suggest that a keyed-access return value starts making more sense than a tuple when there are more than about three members, and especially where there is nesting:

shape["transform"]["raw_matrix"][0, 1] 
# vs.
shape[2][4][0, 1]

That leads into the next question: is the collection going to leave this scope intact, somewhere away from the call that created it? Keyed access over there will absolutely help understandability.

The third question -- reuse -- points to a simple custom datatype as a fourth option that you didn't present.

Is the structure solely owned by this one function? Or are you creating the same dictionary layout in many places? Do many other parts of the program need to operate on this structure? A repeated dictionary layout should be factored out to a class. The bonus there is that you can attach behavior: maybe some of the functions operating on the data get encapsulated as methods.

A fifth good, lightweight, option is namedtuple(). This is in essence the immutable form of the dictionary return value.

2 of 2
1

Don't think about functions returning multiple arguments. Conceptually, it is best to think of functions as both receiving and returning a single argument. A function that appears to accept multiple arguments actually receives just a single argument of tuple (formally product) type. Similarly, a function that returns multiple arguments is simply returning a tuple.

In Python:

def func(a, b, c):
  return b, c

could be rewritten as

def func(my_triple):
  return (my_triple[1], my_triple[2])

to make the comparison obvious.

The first case is merely syntactic sugar for the latter; both receive a triple as an argument, but the first pattern-matches on its argument to perform automatic destructuring into its constituent components. Thus, even languages without full-on general pattern-matching admit some form of basic pattern matching on some of their types (Python admits pattern-matching on both product and record types).


To return to the question at hand: there is no single answer to your question, because it would be like asking "what should be the return type of an arbitrary function"? It depends on the function and the use case. And, incidentally, if the "multiple return values" are really independent, then they should probably be computed by separate functions.

🌐
Python.org
discuss.python.org › python help
Using multiple lists as arguments for python function - Python Help - Discussions on Python.org
September 13, 2021 - İ know it is a little easy but i want you to ask a question i have 2 lists and i want to make a calculation one by one each other in a function. but i got error. could you plese tell me how could i approach this problem. thank you def calcg1(L,T): pi42=4*(math.pi ** 2) g=pi42*L/(T**2) return g T=[1.462,1.434,1.227,1.192,1.256,1.616,2.197] L=[35,30,25,20,15,10,5] results=calcg1(T,L)
🌐
Delft Stack
delftstack.com › home › howto › python › return multiple values python
How to Return Multiple Values From a Function in Python | Delft Stack
February 2, 2024 - Dictionaries are used to store key-value pairs in Python. We can have the final output in a much more organized format by returning a dictionary from a function with keys assigned to different values. See the following example. def return_multi(a): b = a + 1 c = a + 2 return {"b": b, "c": c} x = return_multi(5) print(x, type(x)) ... Classes contain different data members and functions and allow us to create objects to access these members. We can return an object of such user-defined classes based on the class structure and its data members.
🌐
QASource
blog.qasource.com › software-development-and-qa-tips › how-to-return-multiple-values-in-python
How To Return Multiple Values From a Function in Python?
def get_coordinates(): a = 10 b = 20 return a, b # Returning multiple values as a tuple # Example usage coordinates = get_coordinates() print(coordinates) # Output: (10, 20) print(coordinates[0]) # Access x print(coordinates[1]) # Access y · Using Lists: You can return a list if you need a mutable collection.
🌐
McNeel Forum
discourse.mcneel.com › grasshopper
GHPython: how to return multiple lists of data - Grasshopper - McNeel Forum
July 3, 2021 - Hello friends! I am trying to write a script in Python to return a tuple of points, a list of integers and a list of lists with curves. When I return only one list, everything works fine, but if I try to return more than one list, I get only a text mesages like so: IronPython.Runtime.List 120 ...
🌐
Reddit
reddit.com › r/programmerhumor › when you first discover that in python a function can return multiple values
r/ProgrammerHumor on Reddit: When you first discover that in Python a function can return multiple values
October 17, 2019 - This is why the punchline replaces James's usual line "make it double" with "return a tuple" - python """returns""" multiple values by making a tuple not by making it double. ... I get the joke; I was just responding to the title. More replies More replies ... But you might as well just returning a list...