Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
The PEP 484 -- Type Hints says that the types of elements of a tuple can be individually typed; so that you can say Tuple[str, int, float]; but a list, with List typing class can take only one type parameter: List[str], which hints that the difference of the 2 really is that the former is heterogeneous, whereas the latter intrinsically homogeneous.
Also, the standard library mostly uses the tuple as a return value from such standard functions where the C would return a struct.
It's been mentioned that the difference is largely semantic: people expect a tuple and list to represent different information. But this goes further than a guideline; some libraries actually behave differently based on what they are passed. Take NumPy for example (copied from another post where I ask for more examples):
The point is, while NumPy may not be part of the standard library, it's a major Python library, and within NumPy lists and tuples are completely different things.
A direction quotation from the documentation on 5.3. Tuples and Sequences:
The PEP 484 -- Type Hints says that the types of elements of a
tuple
can be individually typed; so that you can sayTuple[str, int, float]
; but alist
, withList
typing class can take only one type parameter:List[str]
, which hints that the difference of the 2 really is that the former is heterogeneous, whereas the latter intrinsically homogeneous.Also, the standard library mostly uses the tuple as a return value from such standard functions where the C would return a
struct
.First of all, they both are the non-scalar objects (also known as a compound objects) in Python.
+
(brand new tuple will be created of course)(3,) # -> (3)
instead of(3) # -> 3
[3]
new_array = origin_array[:]
[x**2 for x in range(1,7)]
gives you[1,4,9,16,25,36]
(Not readable)Using list may also cause an aliasing bug (two distinct paths pointing to the same object).
Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures.
It's been mentioned that the difference is largely semantic: people expect a tuple and list to represent different information. But this goes further than a guideline; some libraries actually behave differently based on what they are passed. Take NumPy for example (copied from another post where I ask for more examples):
The point is, while NumPy may not be part of the standard library, it's a major Python library, and within NumPy lists and tuples are completely different things.
Lists are mutable and tuples are immutable. Just consider this example.
Now change index values of list and tuple.
Hence proved the following code is invalid with tuple, because we attempted to update a tuple, which is not allowed.