How to remove only zero digit from python list?

前端 未结 4 1677
春和景丽
春和景丽 2021-01-19 18:25

I want to remove 0 only from my list:

>>> mylist = [0, 1, None, 2, False, 1, 0]
<
4条回答
  •  情书的邮戳
    2021-01-19 18:53

    You can check for data type of x also and then use != operator.

    # works for integer 0
    [x for x in mylist if type(x) != int or x != 0] 
    # works for float 0.0 as well
    [x for x in mylist if (type(x) != int and type(x) != float) or x != 0]
    

提交回复
热议问题