Python interview question: tuple vs list

Dmitry Belaventsev
Feb 23, 2017 · 1 min read

Let me start the series of “Python interview question” posts with this pretty common question. Tuples and lists are 2 seemingly similar sequence types in Python. And I love this question, because the depth of the answer is a good indicator of the candidate’s experience.

Literal syntax. We use parenthesis (`(` and `)`) to construct tuples and square brackets (`[` and `]`) to get a new list. Also, we can use call of the appropriate type to get required structure — tuple or list.

Mutability. Tuples are immutable, while lists are mutable. This point is the base for following ones.

Memory usage. Due to mutability, you need more memory for lists and less memory for tuples.

Extending. You can add a new element to both tuples and lists with the only difference that the id of the tuple will be changed (i.e., we’ll have new object).

Hashing. Tuples are hashable and lists are not. It means that you can use a tuple as a key in a dictionary.

Semantics. This point is more about best practice. You should use tuples as heterogeneous data structures, while lists are homogeneus sequences.

Links: