Javier Lopez Tomas
Javier Lopez Tomas

Reputation: 2362

Check if a list contains any list

I want to check whether or not a list contains another list inside it. I don't care about the elements, I just want to check the existence of a nested list.

I have done this:

nested_list = False
for element_of_list in b:
    if isinstance(element_of_list, list):
        nested_list = True

But I was wondering if there is a more Pythonic way to do this.

Upvotes: 0

Views: 57

Answers (1)

U13-Forward
U13-Forward

Reputation: 71610

Try using any:

print(any(isinstance(i, list) for i in b))

Upvotes: 3

Related Questions