Here are a few common list operations
adding elements
Add elements using a list of built-in methods append
Number = [1, 2, 3, 4]number.append (5) # number = [1, 2, 3, 4, 5]number.append ([6,7]) # number = [1, 2, 3, 4, 5, [6, 7]]num Ber.append ({' A ': ' B '}) # number = [1, 2, 3, 4, [6, 7], {' A ',: ' B '}
You can see a powerful Python list that can nest any type
List additions
To connect to two lists, you can connect using the + sign
A = [1, 2, 3]b = [4, 5, 6]c = a + b # c = [1, 2, 3, 4, 5, 6]
You can also use the list built-in method extend to connect two lists
A = [1, 2, 3]b = [4, 5, 6]a.extend (b) # a = [1, 2, 3, 4, 5, 6]
Use the + sign to create a new pass object, using extend to modify the original object
List to repeat
The list itself does not remove duplicate functionality, but it can be viewed with another type of Python set (help set)
A = [1, 2, 3, 3,2, 1]b = List (set (a)) # b = [1, 2, 3]
You can also use the built-in method of the dictionary type
A = [1, 2, 2, 3, 1, 3]b = {}.fromkeys (a). Keys () # b = [1, 2, 3]