Home > Article > Backend Development > python list delete elements
python list deletion elements
python Several ways to delete elements
Method 1: Use del method
Python code
>>> names=['Alice','Beth','Cecil' ,'Dee-Dee','Earl']
>>> names
['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']
>> ;> del names[2]
>>> names
['Alice', 'Beth', 'Dee-Dee', 'Earl']
>>>
Python code
>>> numbers=['a', 'b', 'c', 'd']
>>> numbers
['a', 'b', ' c', 'd']
>>> del numbers[1:3]
>>> numbers
['a', 'd']
Method 2: Slice assignment
Python code
>>> numbers=['a', 'b', 'c', 'd']
>>> numbers
[' a', 'b', 'c', 'd']
>>> numbers[1:3]=[]
>>> numbers
['a', 'd ']