1

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??

kojiro
  • 74,557
  • 19
  • 143
  • 201
wonderwhy
  • 393
  • 2
  • 19

2 Answers2

2

If you do not need to edit the return value, use a tuple. The main difference is that lists can be edited.

See this: What's the difference between lists and tuples?

Community
  • 1
  • 1
DrV
  • 22,637
  • 7
  • 60
  • 72
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