I have a function which must return many values (statistics) for other function to interact with them. So I thought about returning them inside a list (array). But then I wondered: should I do so using a list (["foo", "bar"]
) or using a tuple (("foo", "bar")
)? what are the problems or differences there are when using one instead of the other??
Asked
Active
Viewed 62 times
1
2 Answers
2
If you do not need to edit the return value, use a tuple. The main difference is that lists can be edited.
-
No, I don't need to do so. Thanks very much, I didn't know. :=). – wonderwhy Aug 25 '14 at 18:10
2
Use a tuple. In your application, it doesn't seem like you will want or need to change the list of results after.
Though, with many return values you might want to consider returning a dictionary with named values. That way is more flexible and extensible, as adding a new statistic doesn't requiring modifying every single time you use the function.

Roger Fan
- 4,945
- 31
- 38
-
Ah yeah, the dict is the best option, thanks you solved my headache hehe. – wonderwhy Aug 25 '14 at 18:17