I have two lists below
list1 = ['a', 'b', 'c']
list2 = [['a', 'b', 'c'], ['e', 'f', 'g']]
Now I have to find
Is list1 is list of list
Expected result "False"
Is list2 is list of list
Expected result is "True"
How to achieve this
If you want to know if anything in the list is a list:
any(isinstance(a, list) for a in list1)
If you want to know if all the things in the list are lists:
all(isinstance(a, list) for a in list1)
This takes advantage of passing a generator expression to all()
.
any(isinstance(a, list) for a in list1)
It does stop calculation as soon as it finds a True. And it is more readable
list1 = ['a', 'b', 'c']
list2 = [['a', 'b', 'c'], ['e', 'f', 'g']]
print(type(list1[0]))
print(type(list2[0]))
Output:
<class 'str'>
<class 'list'>
[[1,2,3], 1,2,3]
return?