I have to parse some object that is composed of lists. But it can have list within list within list : obj=[[smth1],[[smth2],[smth3]]] each smthX can be a list aswell.
I'm looking for a value that I know is in a "second layer list". In my example, it could be in [[smth2],[smth3]]
What i'm doing right now is iterarating on my object, and testing if what i'm iterating on is a list awell. if it is, I look for my value.
for list in obj :
if isinstance(list, obj) :
for souslist in list :
I LOOK FOR MY VALUE
But I'm reading everywhere (http://canonical.org/~kragen/isinstance/ a lot of stackoverflow thread) that the use of isinstance() is only for special occasion (and my use doesn't look like a special occasion)
Before using isinstance() I was testing what list[0] returned me in a try/except but it felt even wronger. Any alternate way to achieve this in a clean way ? (I don't have any power over the format of my obj I have to work on it)
isinstanceseems fine to me, actually. 99% of my ownisinstanceuse is when I'm iterating through a collection of heterogeneous types, which appears to be exactly what you're doing here. (I'm posting this as a comment and not an answer because it's only my opinion.)isinstancein your case. Your first approach was also ok, as Python believes in "easier to ask for forgiveness than permission": docs.python.org/3.4/glossary.html#term-eafpobj[x][y]it cannot be neitherobj[x]orobj[x][y][z], in fact it will beobj[x][1]