Remove several elements of list by index
- slice
To remove an element by its index, use the slice syntax:
names = ['foo', 'bar', 'baz', 'moo', 'qux']
names[2:4] = []
print(names) # ['foo', 'bar', 'qux']
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
To remove an element by its index, use the slice syntax:
names = ['foo', 'bar', 'baz', 'moo', 'qux']
names[2:4] = []
print(names) # ['foo', 'bar', 'qux']