List / list
A list is a set of data enclosed in square brackets. The elements are separated by commas. It can store any type of objects, such as: list = [1, 'abc', [1, 2, 3], (1, 2, 3), {"A": "apple"}], which is a variable object, so you can add, delete, modify, slice and other operations;
Access list
The elements in the access list are similar to the string. They are accessed according to the subscript (index). The index is positive from left to right. The index starts from 0, followed by 1, 2, 3... And so on; The index starts from - 1 from right to left, followed by - 2, - 3, - 4... And so on. The data obtained after slicing is still a list.
Access element: list [subscript value]
List slice: list [start value: end value: step size] (the default is 1 when the step size is not written)
list1 = [1, 6, 8, 4, 10, 0, 1, 22] print(list1[2]) # Print elements with subscript 2 in the list print(list1[-1]) # Print elements with subscript-1 in the list print(list1[:3]) # Print the element value of the subscript from 0-2 in the list, and the result is still the list print(list1[2:5]) # Print the element values of the list from 2-4, and the slice value range is left open and right closed (that is, the fifth subscript on the right is not included) print(list1[::2]) # Print element values in steps of 2 from beginning to end print(list1[::-1]) # The step size is - 1. Printing elements from the right is equivalent to flashback printing the list print(list1[5:2:-1]) # [0, 10, 4] # The step size is - 1, and the value is taken from right to left. Because it is obtained by positive number, it is the element between subscript 2-5. Because of the principle of opening left and closing right, the data of subscript 2 cannot be obtained, and the data with subscript 5 can be obtained print(list1[5::-1]) # [0, 10, 4, 8, 6, 1] # Step size - 1, output in reverse order, take the element value of subscript 0-5, including the 5th element print(list1[:4:-1]) # [22, 1, 0] # Step size - 1, output in reverse order, take the element value from subscript 4 to the last, and open left and close right. Therefore, the element value with subscript 4 cannot be taken print(list1[:-3:-1]) # Step size - 1, output in reverse order, take subscript - 3 to the last value, open left and close right principle, so the value of subscript - 3 cannot be taken print(list1[-3:]) # Print subscript-3 to the last value
Print results:
8 22 [1, 6, 8] [8, 4, 10] [1, 8, 10, 1] [22, 1, 0, 10, 4, 8, 6, 1] [0, 10, 4] [0, 10, 4, 8, 6, 1] [22, 1, 0] [22, 1] [0, 1, 22]
Update list elements
There are three functions to add data to the list:
List.append(obj) this function has no return value and updates the original list (obj is the object added to the end of the list);
List.insert(index, obj) the function has no return value. Insert a value at the specified position. If the position is not specified, an error will be reported. TypeError: insert expected 2 arguments, got 1 (insert the expected 2 parameters and get 1);
List.extend(seq) this function has no return value. Multiple values in another sequence are best at one time at the end of the list. seq - can be list, tuple, set, dictionary, etc. if it is a dictionary, only the key is added to the end of the list as tuple in turn. If the parameter is empty, an error typeerror: list Extend() takes exactly one argument (0 given) (List.extend() accepts only one parameter (given 0));
list1 = [1, 6, 8, 4, 10, 0, 1, 22] list2 = ['a', 'b', 'c'] dict1 = {'app':'apple', 'ble':'blue'} list1.append(77) # Add 77 to the end of the list print(list1.append(55)) # None no return value list1.insert(55, 44) # Add 4 to the position of subscript 55 list1.extend(list2) # Splice the list list2 into list1 list1.extend(dict1) # extend() adds that the list will only take the key and put it in the list as an element print(list1) # Print results: [1, 6, 8, 4, 10, 0, 1, 22, 77, 44, 'a', 'b', 'c', 'app', 'ble']
Delete list element
There are four functions to delete elements from the list:
del List[index] delete the elements with specified subscripts in the list and update the list,
If the location is not specified, an error SyntaxError: invalid syntax is reported
If the element subscript exceeds the maximum value of the list, an error is reported. IndexError: list assignment index out of range;
Del list [start: end] del can also delete a continuous section of elements in the middle. Start indicates the start subscript and end indicates the end subscript. The elements from start to end will be deleted, but the elements at end position are not included. The principle is similar to the slice value of string and list, which is opened on the left and closed on the right;
List.pop([index = -1]) has a return value. Delete one element of the list and the last element by default. The index cannot exceed the total length of the list, otherwise an error will be reported: IndexError: pop index out of range;
List.remove(obj) has no return value and removes the first match of a certain value in the list;
When there is no obj element in the list, an error is reported: valueerror: list Remove (x): X not in list;
When using this function without filling in obj, an error is reported: typeerror: list Remove() takes exactly one argument (0 given) (list.remove() accepts only one argument (0 given));
List.clear() has no return value. Clear the elements in the whole list, that is, clear the list;
Summary: there are four methods to delete elements in the list, which are divided into three scenarios:
- Delete the element according to the element position index, del and pop() methods;
- Delete according to the element itself, and use the remove() method;
- Delete all elements of the list. clear() method;
# Deletion of list # Note: the printing results of the following codes are the results of printing a single line and commenting out other lines list3 = [1, 6, 8, 4, 10, 0, 1, 22, 77, 44, 'a', 'b', 'c', 'app', 'ble'] del list3[0] # [6, 8, 4, 10, 0, 1, 22, 77, 44, 'a', 'b', 'c', 'app', 'ble'] # del list3[3:5] #[1, 6, 8, 0, 1, 22, 77, 44, 'a', 'b', 'c', 'app', 'ble'] # del list3[90] # IndexError: list assignment index out of range # print(list3.pop(3)) # Return value: 4 # print(list3.pop(66)) # IndexError: pop index out of range # print(list3.remove(1)) # None # list3.remove() # list3 = [6, 8, 4, 10, 0, 1, 22, 77, 44, 'a', 'b', 'c', 'app', 'ble'] # list3.clear() # [] empty list print(list3.clear()) # No return value print(list3)
Other list function methods
Note: the sort() function method is not compared with int and str types, otherwise an error will be reported;
list1 = [1, 6, 8, 4, 10, 0, 1, 22] list2 = ['a', 'b', 'c'] print("list1 Element length of the list:", len(list1)) # Number of print list elements print("list1 The largest element in is:", max(list1)) # Returns the maximum value of the list print("list1 The smallest element in is:", min(list1)) # Returns the minimum value of the list print("list2 The largest element in is:", max(list2)) print("list1 Number of occurrences in 1", list1.count(1)) # The number of occurrences of an element in the sibling list print("list1 Location of the first index in 1:", list1.index(1)) # Find the position of the first matching index of an element in the list print("list1 After sorting:", list1.sort(reverse=False)) # No return value, sort the original list, reverse specifies the sorting rule, True descending, False ascending (default) # print("list1 The reverse order of is:", list1.reverse()) # Reverse sort list print(list1)
Print results:
list1 Element length of the list: 8 list1 The largest element in is: 22 list1 The smallest element in is: 0 list2 The largest element in is: c list1 Number of occurrences in 1 2 list1 Location of the index that first appeared in 1: 0 list1 After sorting: None [0, 1, 1, 4, 6, 8, 10, 22]