Skip to main content
 首页 » 编程设计

python之从列表中删除所有空元素

2025年05月04日9lhb25

<分区>

为什么我的代码没有删除列表中的最后一个空元素?

templist = ['', 'hello', '', 'hi', 'mkay', '', ''] 
 
for element in templist: 
    if element == '': 
        templist.remove(element) 
 
print (templist) 

输出:

['hello', 'hi', 'mkay', '']