Several functions of list operation in python

Lists in Python are variable, which is the most important feature that distinguishes them from tuples and strings. The elements of tuples and strings cannot be modified. Enumerate some common list operation functions and methods.

1. List Append (x) to append x to the end of the list:

1 stack = [3,4,5] #Definition list
2 print(id(stack)) #To print a list id value
3 stack.append(6) #Append 6 to the end of the list
4 print(stack) #Print list
5 print(id(stack)) #Hunting list id value
6 stack.append(7) #Append 7 to list
7 print(stack) #Print list
8 print(id(stack)) #To print a list id value
9 '''Summary: by printing id Discovery, append A new element is added to the original object without changing the storage of the original object in memory.'''

2. List Extend (L) expands the list by adding all elements of the specified iterator, equivalent to a[len(a):] = L, where l must be iteratable, such as list, string, dictionary, set, etc.

 1 #list.extend(L),Expand the list by adding all elements of the specified list, which is equivalent to, list[len(list):] = L
 2 #parameter L Must be iteratable, can be list, tuple, string, dictionary. Collection.
 3 
 4 a = [1] #Define original list
 5 sTring = 'string' #Define string
 6 listNumber = list(range(2,20,3)) #Define a list of numbers in steps of 3
 7 listString = ['a','b','d','e'] #Define string list
 8 dictTa = {'value':120131,'label':'Description','name':'Python',} #Definition dictionary
 9 setTa = set({'value':120131,'label':'Description','name':'Python',}) #Define set
10 
11 print(a) #Print original list a
12 print(id(a)) #Print of the original list id value
13 
14 a.extend(sTring) #String sTring Passed as a parameter to extend,To expand the original list a
15 print(type(sTring)) #Print the data type of the string list
16 print(a) #Print passed extend List after extended string a
17 print(id(a)) #To print an expanded list id Value, and original id Same value
18 
19 a.extend(listNumber) #List numbers listNumber Passed as a parameter to extend,To expand the list a
20 print(type(listNumber)) #Print the data type of the number list
21 print(a) #Print passed extend Expanded list of numbers a
22 print(id(a)) #After printing the expanded numerical list id value
23 
24 a.extend(listString) #Pass a list of strings as parameters to extend,To expand the list a
25 print(type(listString)) #Print data type of string list
26 print(a) #Print passed extend List after expanding string list a
27 print(id(a)) #After printing the extended string list id value
28 
29 a.extend(dictTa) #Translate dictionary dictTa Passed as a parameter to extend,To expand the list
30 print(type(dictTa)) #Data type of print dictionary
31 print(a) #Print passed extend List after expanding dictionary list a
32 print(id(a)) #After printing the extended dictionary id value
33 
34 a.extend(setTa) #Set setTa Passed as a parameter to extend,To expand the list a
35 print(type(setTa)) #Print collection setTa Data type for
36 print(a) #Print passed extend Expanded set list a
37 print(id(a)) #Print extended set id value
38 '''Summary, 1, id The value has not changed, that is, the data storage in memory has not changed;
39         2,Dictionaries and collections via extend Only the key is expanded, and the values are not expanded together;
40         3,Dictionaries and collections via extend Expansion,
41             The keys of the dictionary are arranged in order, that is, the positions in the dictionary are also corresponding in the list after expansion;
42             The keys of the set are arranged randomly;

3. List Insert (i, x) inserts an element at the specified position in the list. The first parameter i specifies the position of the list index, and the second is the value x to be inserted. Such as list Insert (0, x) is to insert the element x, list Insert (2, x) means to insert the element x at the position where the list index is 2; List Insert (len (list), x) means to insert element x at the end of the list, which is equivalent to list Append (x).

 1 a = [1,2,3,4,5]
 2 b = ['4','6','8','10']
 3 c = ['a','b','c','d','e']
 4 d = list(range(1,100,20))
 5 
 6 a.insert(3,6) #In list a Insert the number 6 where the index is 3
 7 a.insert(len(a),7) #In list b Insert the number 7 at the end, which is equivalent to a.append(7)
 8 b.insert(5,'12') #In list b Insert string 12 at index 5
 9 b.insert(len(b),'14') #In list b Insert string 14 at the end, equivalent to b.append('14')
10 c.insert(3,'f') #In list c Insert string at position with index 3'f'
11 c.insert(len(c),'g') #In list c Insert string at the end of g,Equivalent to c.append('g')
12 d.insert(2,10) #In list d Insert the number 10 where the index is 2
13 d.insert(len(d),100) #In list d Insert the number 100 at the end of, which is equivalent to d.append(100)

Another problem is that if the value of the index position specified by the insert is greater than the maximum value of the list index position, that is, list Insert (I, x). When i>len (list), x will be appended to the end of the list, which is equivalent to list Append (x).

4. List Remove (x) deletes the first element in the list with a value of X. If not, a ValueError error error will be raised. For demonstration purposes, create a list of the same elements from beginning to end.

1 #list.remove(x) Delete list list Median is x If the value does not exist, an error will be returned
2 a = ['a','b','c','d','e','a'] #Define the same list of leading and trailing elements for easy demonstration
3 b = a.remove('a') #Delete list a The first value in is a string a And assign values to variables b
4 print(a) #Print list, the first occurrence of a Deleted
5 print(b) #Print b,return None
6 c = a.remove('a') #Re delete a,Just take the last one a Also removed from the list
7 print(a) #Print as expected
8 print(c) #Print c,return None

5. List pop([i]) deletes an element from the specified position in the list (I understand it as popping an element from the specified position in the list). Unlike remove, elements deleted by the pop method can be returned. If the value of the optional parameter [i] is not specified, pop() is deleted from the end of the list. The square brackets around I in the pop([i]) method indicate that this parameter is optional, rather than requiring the input of a square bracket. Such a mark is often encountered in the Python library reference manual. For comparison with remove, the above example is used:

 1 a = ['a','b','c','d','e','a'] #Define the same list of leading and trailing elements for easy demonstration
 2 b = a.remove('a') #Delete list a The first value in is a string a And assign values to variables b
 3 print(a) #Print list, the first occurrence of a Deleted
 4 print(b) #Print b,return None
 5 c = a.remove('a') #Re delete a,Just take the last one a Also removed from the list
 6 print(a) #Print as expected
 7 print(c) #Print c,return None
 8 
 9 d = a.pop(1) #apply pop Method Popup a Value with index 1 in
10 print(a) #Print list a
11 print(d) #Print the pop-up value as a string c

6. List Clear() removes all items in the list, but saves the data type of the list. After using the clear method, the list returns an empty list. Equivalent to del list[:]

 1 a = [1,2,3,4,5] #Define numeric list b
 2 print(id(a)) #Print list a of id value
 3 a.clear() #Method of use clear()Clear all items in the list
 4 print(a) #Print a,Return to empty list
 5 print(type(a)) #Print data type, return<class 'list'>
 6 print(id(a)) #Print list again a of id Value, the same as before deletion
 7 
 8 b = [2,3,4,5,6] #Definition list b
 9 print(id(b)) #Print list b of id value
10 del b[:] #Delete list b In memory copy, b[:]Representation list b A slice of, which can also be understood as a copy.
11 print(b) #Print list b,return[]
12 print(type(b)) #view list b Data type of, return<class 'list'>
13 print(id(b)) # View the cleared list b of id Is the value the same as before emptying
14 
15 c = [6,7,8,9,10] #Definition list c
16 print(c) #Print c,Output:[6, 7, 8, 9, 10]
17 del c #delete c
18 print(c) #Print c,NameError: name 'c' is not defined

Problem: both List b and list c are del statements. List b uses slices, and there are locations in memory; List c deletes the entire object and has no location in memory. To make up for memory, data, storage, object knowledge.

7. List Index (x) returns the index with the first value of X in the list. If not, a ValueError: xxx is not in list error will be returned. Where xxx is the value that caused the error.

 1 a = [1,2,3,4,5] #Define numeric list
 2 b = ['a','b','c','d','e','a'] #Define string list
 3 
 4 r1 = a.index(1) #Get index with value 1
 5 print(r1)    #Print return 0
 6 r1 = a.index(2) #Get list a Index with a median of 2
 7 print(r1)    #Print return 1
 8 r1 = a.index(3) #Get list a Index with a median of 3
 9 print(r1)    #Print return 2
10 r1 = a.index(4) #Get list a Index with a median of 4
11 print(r1)    #Print return 3
12 r1 = a.index(5) #Get list a Index with a median of 5
13 print(r1)    #Print return 4
14 r1 = a.index(6) #Try to get list a The index with a median value of 6 returns an error because the value of 6 does not exist in the list at all ValueError: 6 is not in list
15 
16 
17 #Cyclic read index
18 for index, value in enumerate(a):
19     print(index)
20 
21 for index in range(len(a)):
22     print(index)
23 
24 r2 = b.index('a') #Get list b The first value in is a Index of
25 print(r2)     #Print return 0
26 r2 = b.index('b') #Get list b The first value in is b Index of
27 print(r2)    #Print return 1
28 r2 = b.index('c') #Get list b The first value in is c Index of
29 print(r2)     #Print return 2
30 r2 = b.index('d') #Get list b The first value in is d Index of
31 print(r2)    #Print return 3
32 r2 = b.index('e') #Get list b The first value in is e Index of
33 print(r2)    #Print return 4
34 r2 = b.index('a') #Or get list b The first return value in is a Index of
35 print(r2)     #Print or return 0
36 
37 #Cyclic read index
38 for index, value in enumerate(b):
39     print(index)
40 
41 for index in range(len(b)):
42     print(index)
43     
44     

8. List Count (x) counts the number of times x appears in the list.

 

Tags: Python

Posted by tex1820 on Thu, 02 Jun 2022 09:18:43 +0530