1

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” 
——. . .
5
  • Please remember to turn off "smart quotes" when posting code. Commented Jan 17 at 23:11
  • I’m typing this on a phone sorry Commented Jan 17 at 23:13
  • Can Hello and Hi be at any level of nesting, or do you just need to check at the levels in your example? Commented Jan 17 at 23:13
  • I need the nesting to be exact Commented Jan 17 at 23:14
  • The input is nested, but do you not just need to know that mylist[0] satisfies your criteria? Alternatively, is what you need as a result more like mylist[0][0] + mylist[0][1][0]? Commented Jan 17 at 23:20

3 Answers 3

1

You can just ask whether it's in there:

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

Attempt This Online!

Sign up to request clarification or add additional context in comments.

Comments

1

To check if the specific nested structure exists in mylist without flattening it, you can use recursion to traverse the structure and compare it element by element.

def is_structure_present(haystack, needle):
    if not isinstance(needle, list) or not isinstance(haystack, list):
        return haystack == needle
    
    if len(needle) != len(haystack):
        return False
    
    return all(is_structure_present(h, n) for h, n in zip(haystack, needle))

# Your list
mylist = [
    [
        "Hello",
        [
            "Hi"
        ]
    ]
]

# The structure to check
structure_to_check = [
    "Hello",
    [
        "Hi"
    ]
]

# Check if the structure exists
print(any(is_structure_present(item, structure_to_check) for item in mylist))

3 Comments

They don't need recursion, the items have to be at specific levels in the lists.
Thanks this is exactly what I needed.
Okay this is almost the solution. But it needs to be easily converted to scratch code. So zip() and recursion makes this difficult.
1

Use the any() function and the in operator.

if any("Hello" in sublist and any("Hi" in subsublist for subsublist in sublist if isinstance(subsublist, list)) for sublist in mylist):
    print("Hello and Hi were found")

5 Comments

This won’t work for deeper structures tho
You said it doesn't have to search deeper.
No but what if the structure I’m looking for is deeper
You can just add more nested calls to any(). If you need a more general searching function, you can write a function that takes some representation of the search pattern to look for. It will probably be recursive to be able to drill down dynamically.
You should update the question to describe the more general problem you're trying to solve. It's hard to generalize from just one example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.