Detailed Explanation of Similarities and Differences between Python List and Tuple

  • 2021-07-06 11:32:16
  • OfStack

Preface

The question "What are the differences between list (list) and tuple (tuple)" is often encountered in the interview of junior programmers. Answers that exceed the interviewer's expectations can often add a lot of impression points, and will also provide a certain help for the smooth progress of the follow-up interview. This question mainly examines the candidate's mastery of the basic data structure of Python, which belongs to a simple type of sub-question, so how to answer so as not to lose points?

Similarities: They are all sequence types

Before answering their differences, let's talk about what they have in common. list and tuple are both container objects of sequence type, which can store any type of data and support slicing, iteration and other operations


>>> foos = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> foos[0:10:2]
[0, 2, 4, 6, 8]
>>> bars = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> bars[1:10:2]
(1, 3, 5, 7, 9)

The operation of the two is so similar, why should Python design this type of tuple? It is necessary to find the answer from their differences.

Difference 1: Immutable VS Variable

In addition to the literal difference between the two types (brackets and square brackets), the most important point is that tuple is an immutable type with fixed size, while list is a variable type with dynamic data changes. This difference makes the methods, application scenarios and performance provided by the two types quite different.

List-specific methods:


>>> foo = [2,3,1,9,4]
>>> foo.sort() #  Sort 
>>> foo.insert(5,10) #  Insert 
>>> foo.reverse() #  Reversal 
>>> foo.extend([-1, -2]) #  Expand 
>>> foo.remove(10) #  Remove 
>>> foo.pop() #  Eject last 1 Elements 
>>> foo.append(5) #  Append 

All operations are updated based on the original list, and tuple, as an immutable data type, initializes and iterates tuple faster than list for data of the same size


>python -m timeit "[1,2,3,4,5]"
10000000 loops, best of 3: 0.123 usec per loop
>python -m timeit "(1,2,3,4,5)"
100000000 loops, best of 3: 0.0166 usec per loop

For the same size of data, tuple takes up less memory space


>>> foo = tuple(range(1000))
>>> bar = list(range(1000))
>>> foo.__sizeof__()
8024
>>> bar.__sizeof__()
9088

Atomic tuple objects can also be used as dictionary keys


>>> foo = (1,(2,3))
>>> d = {foo: 1}
>>> bar = (1, [2,3]) #  Non-atomicity tuple Because the tuple contains a non-hashable list
>>> d = {bar: 1}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Difference 2: Isomorphic VS Heterogeneous

tuple is used to store heterogeneous (heterogeneous) data as a record without field name, for example, tuple is used to record a person's height, weight and age.


person = ("zhangsan", 20, 180, 80)

For example, record a point on coordinates


point = (x, y)

Listing 1 is used to store isomorphic data (homogenous), which means data with the same meaning, for example, the following are all string types


["zhangsan", "Lisi", "wangwu"]

Another example is the multiple user records stored in list


[("zhangsan", 20, 180, 80), ("wangwu", 20, 180, 80)]

Because the use of tuple as an unnamed record has certain limitations in some scenarios, there is another namedtuple type, and namedtuple can specify the field name and be used as a lightweight class.


Related articles: