# What is the distinction between a list and a tuple?

## Problem

What’s the difference?

What are the benefits and drawbacks of tuples and lists?

## Solution #1

Apart from the fact that tuples are immutable, there is a semantic distinction that should be considered while using them. Lists are homogeneous sequences, but tuples are heterogeneous data structures (i.e., their entries have diverse meanings). Lists have order, while tuples have structure.

Using this distinction clarifies and simplifies the code.

Pairs of page and line numbers, for example, can be used to refer to specific areas in a book, such as:

``````my_location = (42, 11)  # page number, line number
``````

You may then use this as a key in a dictionary to keep track of where you’ve been. On the other hand, a list can be used to keep track of several locations. Because it’s natural to wish to add or remove locations from a list, it’s no surprise that lists are mutable. Tuples, on the other hand, are immutable since it makes no sense to add or remove things from an existing place.

When iterating through the lines of a page, there may be times when you want to update things within an existing location tuple. Tuple immutability, on the other hand, necessitates the creation of a new location tuple for each new value. On the surface, this appears cumbersome, yet using immutable data like this is a cornerstone of value types and functional programming techniques, both of which can provide significant benefits.

There are some interesting articles on this issue, e.g. “Python Tuples are Not Just Constant Lists” or “Understanding tuples vs. lists in Python”. This is also mentioned in the official Python manual.

In a statically typed language like Haskell, a tuple’s values are usually of distinct types, and the tuple’s length must be fixed. The values in a list are all of the same type, and the length is not fixed. As a result, the distinction is clear.

Finally, Python has a namedtuple, which makes sense because a tuple is supposed to have structure. Tuples are a lightweight alternative to classes and instances, as evidenced by this.

## Solution #2

Difference between list and tuple

## Solution #3

If you went on a walk, you could record your coordinates in a (x,y) tuple at any time.

If you wanted to keep track of your journey, you could add your location to a list every few seconds.

However, you wouldn’t be able to do it the other way around.

## Solution #4

Tuples are distinct in that they are immutable. This means that after you’ve generated a tuple, you can’t change its values.

If you’re going to need to alter the values, a List is the way to go.

Benefits to tuples: