使用for循环倒序删除,防止跳过元素
"""
删除列表中所有奇数
"""
list01=[54,5,65,67,78,8]
for i in range(len(list01) - 1, -1, -1):
if list01[i] % 2 != 0:
del list01[i]
print(list01)
使用for循环倒序删除,防止跳过元素
"""
删除列表中所有奇数
"""
list01=[54,5,65,67,78,8]
for i in range(len(list01) - 1, -1, -1):
if list01[i] % 2 != 0:
del list01[i]
print(list01)