Getting Started with Python: Understanding Lists and Tuples

  • 2021-12-09 09:37:52
  • OfStack

Characteristics of catalog list, characteristics of tuple, operations supported by both, negative index slicing operation, random nesting and mutual conversion, differences in storage methods of commonly used built-in functions, comparison of their usage scenarios, summary of questions: Which of the two methods to create an empty list is faster.

List and tuple are both an ordered collection that can place any data type.

Characteristics of lists

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

The elements in the original list are modified without creating a new list.


#  New 1 List 
l = [1, 2, 'hello', 'world']
#  Output list 
l
#  Display content 
[1, 2, 'hello', 'world']
#  Access list element 
l[3] = 3 #  List allows access to the 4 Elements, and modify 
#  Output list 
l
#  Display content 
[1, 2, 'hello', 3]
#  Add Element 
l.append(5) #  Put the element 5  Add to the back of the list 
#  Output list 
l
#  Display content 
[1, 2, 'hello', 3, 5]

Characteristics of tuples

Static (immutable): The length is fixed and cannot be added, deleted or changed.

To change the data in a tuple, you can only open a new block of memory and create a new tuple.


#  New 1 Tuples 
tup = ('jason', 22)
#  Output tuple 
tup
#  Display content 
('jason', 22)
#  You cannot operate tuples directly, you need to create a new one 1 New tuples 
net_tup = tup + (5, ) #  Create a new tuple new_tup  After the element is added, fill the element with it, and the ','  Can not be omitted 
#  Output tuple 
net_tup
#  Display content 
('jason', 22, 5)

Operations supported by both

Negative index

Both lists and tuples support negative indexing, with-1 for the penultimate element,-2 for the penultimate element, and so on.


l = [1, 2, 'hello', 3, 5]
l[-1] #  Negative index of list 
#  Display content 
5
tup = ('jason', 22)
tup[-1] #  Negative index of tuples 
#  Display content 
22

Slicing operation

The slicing operation is to output all elements within the specified range [start: end], but not the last 1 bit.

For example, [0: 3] starts with subscript 0, and 1 goes straight to subscript 2 (3-1).


l = [1, 2, 'hello', 3, 5]
l[0:3] #  Slicing operation of list 
#  Display content 
[1, 2, 'hello']
tup = ('jason', 22)
tup[0:2] #  Slicing operation of tuples 
#  Display content 
('jason', 22)

Random nesting


new_l = [[1, 2, 3], [4, 5]] #  Nested list 
new_l
#  Display content 
[[1, 2, 3], [4, 5]]
new_tup = ((1, 2, 3,), (4, 5, 6)) #  Nested tuple 
new_tup
#  Display content 
((1, 2, 3), (4, 5, 6))

Mutual conversion


list((1, 2, 3)) # list()  Function converts tuples into lists 
tuple([1, 2, 3]) # tuple()  Function converts a list into a tuple 

Commonly used built-in functions

count (item) Count the number of occurrences of item in a list/tuple

index (item) returns the index of the first occurrence of item in the list/tuple list. reverse () and list. sort () can only be applied to lists list. reverse () Reverse list in place list. sort () sort reversed() and sorted() reversed () reverses list/tuple Returns 1 inverted iterator and converts it to a list using list () sorted () sorts lists/tuples Returns a new ordered list

Differences in storage methods


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

Stores the same content, but the list uses 16 bytes more than tuples.

The list is dynamic and requires a pointer to point to the corresponding element. The list is variable, requires additional storage of the allocated length, tracks the usage of list space, and allocates additional space in time when space is insufficient. The storage space of the space length of storing pointers is 8 bytes each.

The initial space of the list is: 40. Adding characters expands the space of 4 elements: 32, so it is: 72 bytes. When the element space needed to be stored is larger than the storage space, the list will add 4 elements: 32.

The addition/deletion mechanism (over-allocating) ensures the effectiveness of its operation, and the time complexity of addition/deletion is the same as that of O (1)

Performance comparison

Tuples are more lightweight than lists, and overall, tuples perform slightly faster than lists.

Python does some resource caching of static data in the background (resource caching)

Garbage collection mechanism, when a number of variables are not used, Python will recycle their occupied memory, returned to the operating system for other variables or other applications to use.

However, Python temporarily caches this 1 part when the tuple is not used and takes up a small amount of space. On the next call, Python will use this cached memory space directly, greatly speeding up the running speed of the program.

Initializes 1 list and tuple of the same element, and the tuple is 5 times faster than the list.

Index operation, the speed difference between them is very small and almost negligible.

Add/delete or change elements, and the list is better. Tuple requires a new tuple.

Use scenarios of both

The stored data and quantity are unchanged, and tuples are suitable. The data or quantity stored is variable, and the selection list is more appropriate.

Summarize

Lists and tuples are ordered and can store collections of any data type.

Difference:

The list is dynamic and variable in length. Elements can be added, deleted and modified. The storage space is slightly larger than tuples and the performance is slightly inferior to tuples.

Tuples are static and fixed in length. You can't operate on elements, and tuples are lightweight and slightly better for lists.

Question: Which of the two ways to create an empty list is faster.


# option A
empty_list = list()
# option B
empty_list = []

Self-Answer: I think option B is a little faster, and it directly creates an empty list. But option A is converted through list ().

Answer: [] is a built-in C function, which is called directly. While list is an function call, function call of Python will create stack, and perform a series of parameter checking operations to compare expensive.

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: