0

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.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41
  • Does this answer your question? [Using any() and all() to check if a list contains one set of values or another](https://stackoverflow.com/questions/19211828/using-any-and-all-to-check-if-a-list-contains-one-set-of-values-or-another) – mkrieger1 Jul 21 '21 at 10:08
  • Rather: https://stackoverflow.com/questions/1342601/pythonic-way-of-checking-if-a-condition-holds-for-any-element-of-a-list – mkrieger1 Jul 21 '21 at 10:11

1 Answers1

3

Try using any:

print(any(isinstance(i, list) for i in b))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114