Search code examples
pythonlistfilternull

Python remove null items from list


I created the following list in python for DynamoBIM:

a = [["f", "o", "c"], [null, "o", null], [null, "o", null]]

I want to remove the null items from this list to create this list:

a = [["f", "o", "c"], ["o"], ["o"]]

I've attempted list.remove(x), filters, for-loops, and a number of other methods but cannot seem to get rid of these buggers.

How can I do this?


Solution

  • Actually Null is not in python, their it should be string like

    list_value = [["f", "o", "c"], ['null', "o", 'null'], ['null', "o", 'null']]
    
    [filter(lambda x: x!='null' and x!=None, inner_list) for inner_list in list_value]
    
    [['f', 'o', 'c'], ['o'], ['o']]
    

    You could also solve by nested list comprehension:

    [[for i in inner_list if i!='null' and not i] for inner_list in list_value]