python - difference remove, del, pop three kinds of ways of removing elements

Bad memory, sorted out in order to save as

1、remove

① delete elements, remove (obj), delete the first encounter of the order, so I want to delete all, you need to traverse

aList = [123, 'xyz', 'zara', 'abc', 'abc'];

aList.remove('xyz');
print(aList)
aList.remove('abc');
print(aList)

>>>
[123, 'zara', 'abc', 'abc']
[123, 'zara', 'abc']

 

 

 2、pop

Variables pop elements can be defined to catch

① no specified index is deleted from the tail

= of aList [123, ' XYZ ' , ' ZARA ' , ' ABC ' , ' ABC ' ]; 

A = aList.pop ()          # from the rear pop-up element 
Print (of aList) 
Print (A) "" " [
123, ' XYZ ' , ' ZARA ' , ' ABC ']
ABC

  

② specified index, using the index position to delete

a = aList.pop(1)
print(a)
print(aList)

>>>
xyz
[123, 'zara', 'abc', 'abc']

 

3, the

① can use the list [index] delete position

del list [index]

aList = [123, 'xyz', 'zara', 'abc', 'abc']

del aList[0]
print(aList)

》》》
['xyz', 'zara', 'abc', 'abc']

 

 

② can delete sections

of aList = [123, ' XYZ ' , ' ZARA ' , ' ABC ' , ' ABC ' ] 

del of aList [0: 2]          # Left closed right open interval    
Print (of aList)         

"" " 
[ ' ZARA ' , ' ABC ' , ' ABC ' ]    

 

③ can delete the entire list

del list

Guess you like

Origin www.cnblogs.com/fish-101/p/11366134.html