Python 中删除列表中所有的空元素

Python 中删除列表中所有的空元素

1. 删除None值

mylist = ["asas", "cfdsgfdas", None, "csdfew"]
while None in mylist:
    mylist.remove(None)
des = " ".join(mylist)
print(des)
 mylist = ["asas", "cfdsgfdas", None, "csdfew"]
 mylist2 = []
 for var in mylist:
     if var!=None:
         mylist2.append(var)
 mylist = mylist2
 des = " ".join(mylist)
 print(des)

2. 删除’ ‘值

while ' ' in mystr:
    mystr.remove(None)

你可能感兴趣的:(python列表,删除列表空元素,Python)