List vs tuple, when to use each?

list, python, tuples, types

Solution

There's a strong culture of tuples being for heterogeneous collections, similar to what you'd use `struct`s for in C, and lists being for homogeneous collections, similar to what you'd use arrays for. But I've never quite squared this with the mutability issue mentioned in the other answers. Mutability has teeth to it (you actually can't change a tuple), while homogeneity is not enforced, and so seems to be a much less interesting distinction.

Problem

In Python, when should you use lists and when tuples? Sometimes you don't have a choice, for example if you have ``` "hello %s you are %s years old" % x ``` then x must be a tuple. But if I am the one who designs the API and gets to choose the data types, then what are the guidelines?

Original source

Related problems