Is there any ways we can detect if an object is list in python using type(obj) --> list.
But how can we detect if the object is list of list of the form as:
[['a','b']['a','b'][][]] Use isinstance() to check for a specific type:
>>> isinstance([], list) True Use all() to test if all elements are of a certain type:
all(isinstance(elem, list) for elem in list_of_lists) all() short-circuits; if any of the tests returns False, the loop is terminated and False is returned. Only if all but one element returns True does all() need to examine every element of the iterable.
If you want to make sure that every item in your list is a list, you could do something like this:
if all(isinstance(i, list) for i in lst): # All of the items are lists isinstance(i, list) is the better way of writing type(i) == type(list) or type(i) == list).all() returns True if all of the items in the sequence are True. It'll return False if any aren't True.type(i) == type(list) newer works anyway. :-P type(i) is list can have some merits; it tests for the exact type (no subclasses allowed).