Methods python basic data types (LIST)

1.list.append()

Note: Additional elements to the list, the default is the end of the list.

Example:

= LST [] # initialization list 
lst.append (. 1) # additive element 
lst.append (2)
Print (LST) # print element 
# results [1, 2]

2.list.extend()

Description: extension, a plurality of data added to the list (available iteration), such as a list of dictionary string (default key) set of tuples

Example:

= LST [1,2] 
lst.extend ({ "name": ". 11", "Age":}. 19) # dictionary 
lst.extend ([3,4,5]) # listing 
lst.extend ((6, 7)) # tuple 
lst.extend ({8,9}) # set 
lst.extend ( "101112") # string a string is added to each element into the 
Print (LST) 
# results [1, 2 ' name ',' age ', 3 , 4, 5, 6, 7, 8, 9,' 1 ',' 0 ',' 1 ',' 1 ',' 1 ',' 2 ']

3.list.remove()

Description: delete an element, if you need to remove the element was not in the list, an error #ValueError: list.remove (x): x not in list

Example:

= LST [1,2,3,4] 
lst.remove (. 1) 
Print (LST) 
# Results: [2, 3, 4]

4.list.pop()

Description: Delete by the index element and return, if you want to delete the index value was not in the list, an error #IndexError: pop index out of range

Example:

= LST [1,2,3,4] 
A = lst.pop (3) 
Print (LST) 
Print (A) 
# Results: 
[1, 2, 3] 
4

5.list.insert()

Description: Insert element according to the index value

Example:

= LST [1,2,3,4 ] 
lst.insert (0, 5) # 0 denotes Index 
Print (LST)
 # Results: [5, 1, 2, 3, 4]

6.list.reverse()

Description: Flip list

Example:

lst = [1,2,3,4]
lst.reverse()
print(lst)

7.list.sort()

Description: List Sorting

lst = [4,3,2,1]
lst.sort()
print(lst)

Guess you like

Origin www.cnblogs.com/jinyan-huang/p/11373206.html