pythonnested-lists

How to check for specific structure in nested list python


Suppose we have the list:

mylist = [
    [
        "Hello",
        [
            "Hi"
        ]
    ]
]

How do I check that list containing "Hello" and "Hi" exists in mylist, in specifically this structure without flattening it?

All the solutions are flattening the list, but I need to check for specific structure, like this

Array
|_
—-|_ “Hello”
———|_ “Hi” 
——. . .

Solution

  • You can just ask whether it's in there:

    ["Hello", ["Hi"]] in mylist
    

    Attempt This Online!