Search code examples
pythonlistnestedpython-itertoolsnonetype

Check if None in nested list


How to find a None in a nested listed. When I run the following code, the Hello is not printed. I do not know how to spot a None in the list of a list. Can you please help me with this simple question.

myList = [[0,1,2],[5,None,300]]

if None in myList:

print("Hello")

Solution

  • You could use any:

    myList = [[0,1,2],[5,None,300]]
    if any(None in l for l in myList):
        print("Hello")
    

    Or itertools.chain:

    from itertools import chain
    if None in chain(*myList):
        print("Hello")