a script is feeding me with list of list of list or list of list. What I plan to do is call this
test = myList[0][0][0]
and if an exception is raised I'll know that it's a list of list.
Is there a better/proper way to do this?
Thanks.
a script is feeding me with list of list of list or list of list. What I plan to do is call this
test = myList[0][0][0]
and if an exception is raised I'll know that it's a list of list.
Is there a better/proper way to do this?
Thanks.
I'm not sure if it's better/proper, but you can also test whether something is a list with isinstance
or type
functions.
For example
a = [1,2,3]
b = (1,2,3) # Not a list
type(a) == type([]) # True
type(b) == type([]) # False
type(a) is list # True
type(b) is list # False
isinstance(a, list) # True
isinstance(b, list) # False
The first method is probably not ideal, the second would probably be better if you were to use type
, but I think the general consensus is that isinstance
is generally better.
EDIT: Some discussion about the difference between the two approaches
So, I guess your code would look something like:
if(isinstance(myList[0][0], list)):
# Use myList[0][0][0]
else:
# Use myList[0][0]