List IntroductionList is the most frequently used data type in Python.
The data types in the List can be arbitrary and dynamic expansion is supported.
Simple definition of List: Lis = ['A', 'B', 'abc', 'asd ']. List is a set of ordered elements enclosed by square brackets.
List supports two index modes: normal index mode, starting from 0.
Negative indexes are counted from the end of the List. The last original of any non-empty List is always List [-1].
List supports sharding, which is used to retrieve data from the List. Note the starting position of Slice.
Add data to the List: Lis. Append ('new') add data to the end of the List;
Lis. insert (2, 'new') inserts a value at the position of List 2;
Lis. extend (['new', 'nwe']) links to a New List (at the end) in the original List ).
Differences between Append () and Extend:
The Append () parameter can be of any data type, but can also be a List, but it regards how List as an element and adds it to the original List.
The parameter of Extend () can only be a List, and all elements of the entire List are added to the original List one by one.
Search in List: Lis. index ('A ')
Index searches for a value in the List and returns its first index value. If it appears multiple times, only the first index value is returned. If it is not in the List, an exception is returned.
Test whether a value is In the List. The value returned by using in: >>> 'C' In lis is False.
Delete all elements in the List:
Lis. remove ('A') first appearance of deleting a value from the List (not all ).
Lis. pop () will do two things: Delete the last element of the List and return the deleted element.
List can be linked with the + operator. List = List + otherList is equivalent to list. extend (otherList ). However, the + operator uses a new list (after link) as the return value, while extend () only modifies the existing List. Therefore, for large lists, the execution speed of Extend () is faster.
List supports the + = Operator.
* The operator can act on the List as a repeat. Lis = [1, 2] * 3 is equivalent to lis = [1, 2] + [1, 2] +. That is, link the three lists into one.
- Tuple Introduction
Tuple is an unchangeable List. Once a Tuple is created, it cannot be changed in any way.
Simple definition of Tuple: Tup = ('A', 'B', 'abc', 'asd ') the entire element set is surrounded by parentheses.
Because Tuple is immutable, there is no way to add or delete elements. You can partition indexes in the same way as List indexes. When a List is split, a new List is obtained. When a Tuple is split, a new Tuple is obtained.
Tuple can also use the in method to check whether an element exists in Tuple.
Tuple can be considered as a special List, which has the following advantages:
Tuple is faster than List operations. To define a constant set, you only need to use Tuple for storage. Your unique operation on it is to continuously traverse it.
Tuple can be seen as "Write protection" for data that does not need to be changed ". This makes the code more secure.
Tuple can be used as the Key in Dictionary, while List cannot! Because the key in Dictionary must be unchangeable.
Tuple can be converted to List. The built-in tuple function can receive a List and return a Tupelo with the same elements. The List function receives a Tuple and returns a List.