Home > Backend Development > Python Tutorial > How to Delete List Elements by Index in Python?

How to Delete List Elements by Index in Python?

Susan Sarandon
Release: 2024-12-24 12:40:36
Original
183 people have browsed it

How to Delete List Elements by Index in Python?

How to Delete List Elements by Index

Question:

How to delete List Elements by Index Remove element from ?

Answer:

Use del and specify the index of the element to be deleted:

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del a[-1]
print(a)  # Output: [0, 1, 2, 3, 4, 5, 6, 7, 8]
Copy after login

del also supports slicing, which is used to delete a range of elements:

del a[2:4]
print(a)  # Output: [0, 1, 4, 5, 6, 7, 8, 9]
Copy after login

For more details, please see the following section of the tutorial:

The above is the detailed content of How to Delete List Elements by Index in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template