Given a list and we have to remove elements in a range from the list in Python.
给定一个列表,我们必须从Python中的列表中删除范围内的元素。
删除列表(开始索引,结束索引) (del list(start_index, end_index))
del() method is used to remove all elements of list in range starting from start_index to end_index.
del()方法用于删除从start_index到end_index范围内的list的所有元素。
Syntax:
句法:
del list(start_index, end_index)
Program:
程序:
# Declaring a list
list = [10, 20, 30, 40, 50]
# print list
print "List element:"
for l in range(len(list)):
print (list[l])
# delete element from index 1 to 30del list[1.3]
del list[1:3]
# print list after deleting
# element from index 1 to 3
print "List element after del[1:3]:"
for l in range(len(list)):
print (list[l])
Output
输出量
List element:
10
20
30
40
50
List element after del[1:3]:
10
40
50
翻译自: https://www.includehelp.com/python/remove-all-elements-in-a-range-from-the-list.aspx