This article mainly introduces the detailed operation methods of List in Python, including creation, access, update, deletion, and other operations. Friends who need it can refer to
List is the most basic data structure in Python. List is the most commonly used Python data type. The data items of the list do not need to have the same type. Each element in the list is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
Python has 6 built-in types for sequences, but the most common are lists and tuples. Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of the sequence and determining the largest and smallest elements.
One, create a list
As long as the different data items separated by commas are enclosed in square brackets. As follows:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];
Like the index of a string, the list index starts at 0. The list can be intercepted, combined, etc.
Two, access the value in the list
Use the subscript index to access the values in the list. Similarly, you can also use square brackets to intercept characters, as shown below:
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]
The output of the above example:
list1[0]: physics
list2[1:5]: [2, 3, 4, 5]
Three, update the list
You can modify or update the data items of the list, and you can also use the append() method to add list items, as shown below:
#!/usr/bin/python
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];
The output of the above example:
Value available at index 2 :
1997
New value available at index 2 :
2001
Fourth, delete list elements
You can use the del statement to delete elements of the list, as in the following example:
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;
The output of the above example:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]
Five, Python list script operators
The operators of the list pairs + and * are similar to strings. The + sign is used for combined lists, and the * sign is used for repeated lists.
As follows:
Python expression | result | description |
---|---|---|
len ([1, 2, 3]) | 3 | length |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | combination |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | repeat |
3 in [1, 2, 3] | True | Whether the element exists in the list |
for x in [1, 2, 3]: print x, | 1 2 3 | Iteration |
Six, Python list interception
Python's list interception and string manipulation types are as follows:
L = ['spam', 'Spam', 'SPAM!']
operating:
Python expression | result | description |
---|---|---|
L[2] | 'SPAM!' | Read the third element in the list |
L[-2] | 'Spam' | Read the penultimate element in the list |
L[1:] | ['Spam', 'SPAM!'] | Intercept the list from the second element |
Seven, Python list operations functions and methods
List operations include the following functions:
1. cmp(list1, list2): compare the elements of two lists
2. len(list): the number of list elements
3. max(list): return the maximum value of the list elements
4. min(list) : Return the minimum value of the list element
5. list(seq): convert the tuple into a list
List operations include the following methods:
1. list.append(obj): add a new object at the end of the list
2. list.count(obj): count the number of times an element appears in the list
3. list.extend(seq): Add multiple values in another sequence at the end of the list at once (expand the original list with the new list)
4. list.index(obj): find the index position of the first matching item of a certain value from the list
5. list.insert(index, obj): insert the object into the list
6, list.pop(obj=list[-1]): remove an element from the list (the last element by default), and return the value of the element
7, list.remove(obj): remove the first matching item of a value in the list
8, list.reverse(): reverse the element in the list
9, list.sort([func]): sort the original list