The difference and connection between lists and tuples in Python

First look at what is a list? What is a tuple?

Lists and tuples are both an ordered collection of any data type. In most programming languages, the data types of collections must be consistent. However, there is no such requirement for Python lists and tuples.

List:

l = [1, 2, 'hello', 'world'] 
l

[1, 2, 'hello', 'world']

Tuple:

tup = ('jason', 22)
tup

('jason', 22)

the difference

The list is dynamic, the length is not fixed, and the elements can be added, deleted or changed at will (mutable).

Tuples are static, have a fixed length, and cannot be added, deleted, or changed (immutable).

l = [1, 2, 3, 4]
l[3] = 40 # 和很多语言类似,python中索引同样从0开始,l[3]表示访问列表的第四个元素
l
[1, 2, 3, 40]

tup = (1, 2, 3, 4)
tup[3] = 40
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

But what if you want to make any "changes" to the existing tuples? Then you can only reopen a piece of memory and create a new tuple.

tup = (1, 2, 3, 4)
new_tup = tup + (5, ) # 创建新的元组new_tup,并依次填充原元组的值
new _tup
(1, 2, 3, 4, 5)

l = [1, 2, 3, 4]
l.append(5) # 添加元素5到原列表的末尾
l
[1, 2, 3, 4, 5]

Same points

Both support negative index, -1 means the last element, -2 means the penultimate element, and so on.

l = [1, 2, 3, 4]
l[-1]
4

tup = (1, 2, 3, 4)
tup[-2]
3

All support slicing operations

l = [1, 2, 3, 4]
l[1:3] # 返回列表中索引从1到2的子列表
[2, 3]

tup = (1, 2, 3, 4)
tup[1:3] # 返回元组中索引从1到2的子元组
(2, 3) 

Can be nested at will

l = [[1, 2, 3], [4, 5]] # 列表的每一个元素也是一个列表

tup = ((1, 2, 3), (4, 5, 6)) # 元组的每一个元素也是一个元组

Can be converted to each other through the list() and tuple() functions

list((1, 2, 3))
[1, 2, 3]

tuple([1, 2, 3])
(1, 2, 3)

Some commonly used built-in functions for lists and tuples

l = [3, 2, 3, 7, 8, 1]
l.count(3) 
2
l.index(7)
3
l.reverse()
l
[1, 8, 7, 3, 2, 3]
l.sort()
l
[1, 2, 3, 3, 7, 8]

tup = (3, 2, 3, 7, 8, 1)
tup.count(3)
2
tup.index(7)
3
list(reversed(tup))
[1, 8, 7, 3, 2, 3]
sorted(tup)
[1, 2, 3, 3, 7, 8]
  • count(item) represents the number of occurrences of item in the count list/tuple.
  • index(item) represents the index of the first occurrence of item in the returned list/tuple.
  • list.reverse() and list.sort() represent in-situ list reversal and sorting respectively (note that tuples do not have these two built-in functions).
  • reversed() and sorted() also mean to reverse and sort the list/tuple, reversed() returns a reversed iterator (the example above uses the list() function to convert it to a list); sorted() returns New sorted list.

Differences in storage of lists and tuples

Lists are dynamic and mutable, while tuples are static and immutable. This difference will inevitably affect the storage methods of the two.

l = [1, 2, 3]
l.__sizeof__()
64
tup = (1, 2, 3)
tup.__sizeof__()
48

The list is dynamic, so it needs to store a pointer to point to the corresponding element (in the above example, for the int type, 8 bytes). In addition, because the list is variable, additional storage of the allocated length (8 bytes) is required, so that the usage of the list space can be tracked in real time, and additional space can be allocated in time when the space is insufficient.

Through an example, to describe the process of list space allocation.

l = []
l.__sizeof__() // 空列表的存储空间为40字节
40
l.append(1)
l.__sizeof__() 
72 // 加入了元素1之后,列表为其分配了可以存储4个元素的空间 (72 - 40)/8 = 4
l.append(2) 
l.__sizeof__()
72 // 由于之前分配了空间,所以加入元素2,列表空间不变
l.append(3)
l.__sizeof__() 
72 // 同上
l.append(4)
l.__sizeof__() 
72 // 同上
l.append(5)
l.__sizeof__() 
104 // 加入元素5之后,列表的空间不足,所以又额外分配了可以存储4个元素的空间

It can be seen that in order to reduce the overhead of space allocation during each increase/decrease operation, Python allocates more space each time it allocates space. This mechanism (over-allocating) ensures the efficiency of its operation: increase/ The time complexity of deletion is O (1) O(1)O ( 1 )

For tuples, the situation is different. The length of the tuple is fixed, and the elements are immutable, so the storage space is fixed.

Performance of lists and tuples

Tuples are more lightweight than lists, so overall, the performance speed of tuples is slightly better than that of lists.

Python will do some resource caching for static data in the background. Generally speaking, because of the garbage collection mechanism, if some variables are not used, Python will reclaim the memory they occupy and return it to the operating system for other variables or other applications to use.
But for some static variables, such as tuples, if it is not used and takes up little space, Python will temporarily cache this part of the memory. In this way, next time we create a tuple of the same size, Python can no longer request the operating system to find memory, but can directly allocate the previously cached memory space, which can greatly speed up the running speed of the program.

The following example is to calculate the time required to initialize a list and tuple of the same element. We can see that the initialization speed of tuples is 5 times faster than that of lists.

python3 -m timeit 'x=(1,2,3,4,5,6)'
20000000 loops, best of 5: 9.97 nsec per loop
python3 -m timeit 'x=[1,2,3,4,5,6]'
5000000 loops, best of 5: 50.1 nsec per loop

But if it is an index operation, the speed difference between the two is very small, almost negligible.

python3 -m timeit -s 'x=[1,2,3,4,5,6]' 'y=x[3]'
10000000 loops, best of 5: 22.2 nsec per loop
python3 -m timeit -s 'x=(1,2,3,4,5,6)' 'y=x[3]'
10000000 loops, best of 5: 21.9 nsec per loop

Add, delete or change elements, then the list is obviously better. Because for tuples, you have to do it by creating a new tuple, which is definitely more expensive.

Use cases for lists and tuples

  1. If the stored data and quantity remain the same, for example, if you have a function that needs to return the latitude and longitude of a location, and then pass it directly to the front-end rendering, then it is more appropriate to use tuples.
def get_location():
    ..... 
    return (longitude, latitude)
  1. If the stored data or quantity is variable, for example, a log function on a social platform is to count which users' posts a user has read within a week, then a list is more appropriate.
viewer_owner_id_list = [] # 里面的每个元素记录了这个viewer一周内看过的所有owner的id
records = queryDB(viewer_id) # 索引数据库,拿到某个viewer一周内的日志
for record in records:
    viewer_owner_id_list.append(record.id)

Small summary

difference:

  • The list is dynamic, the length is not fixed, and the elements can be added, deleted or changed at will (mutable). The tuple is static, the length is fixed, and it cannot be added, deleted, or changed (immutable).
  • The storage space of the list is slightly larger than that of the tuple, and its performance is slightly inferior to that of the tuple. Compared with the list, the tuple is more lightweight and has slightly better performance.

Common ground

  • Both lists and tuples in Python support negative indexing, -1 means the last element, -2 means the penultimate element;
  • Both lists and tuples support slicing operations;
  • Both lists and tuples can be nested at will;
  • Both lists and tuples are ordered and can store collections of any data type.

Guess you like

Origin blog.csdn.net/qq_41485273/article/details/114030018