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
Susan SarandonOriginal
2024-12-24 12:40:36180browse

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]

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]

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:
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