在 Python 中,可以通过多种不同的方法从列表中删除空值。要从列表中删除空字符串,我们将使用filter()、join()和remove()函数。那么让我们看看下面的示例。
那么让我们看看下面的例子:
示例 1:
main.py
myList = ['', 'Tuts-Station.com', '', 'is', 'Best', ''] # Remove Empty Value from List newList = list(filter(None, myList)) print(newList)
输出
['Tuts-Station.com', 'is', 'Best']
示例 2:
main.py
myList = ['', 'Tuts-Station.com', '', 'is', 'Best', ''] # Remove Empty Value from List newList = ' '.join(myList).split() print(newList)
输出
['Tuts-Station.com', 'is', 'Best']
示例 3:
main.py
myList = ['', 'Tuts-Station.com', ' ', 'is', 'Best', ''] # Remove Empty Value from List while("" in myList) : myList.remove("") print(myList)
输出
['Tuts-Station.com', 'is', 'Best']
它会帮助你……