Checking if type == list in python
I may be having a brain fart here, but I really can't figure out what's wrong with my code:
for key in tmpDict: print type(tmpDict[key]) time.sleep(1) if(type(tmpDict[key])==list): print 'this is never visible' break
the output is
<type 'list'> but the if statement never triggers. Can anyone spot my error here?
Your issue is that you have re-defined
list as a variable previously in your code. This means that when you do
type(tmpDict[key])==list if will return
False because they aren't equal.
That being said, you should instead use
isinstance(tmpDict[key], list) when testing the type of something, this won't avoid the problem of overwriting
list but is a more Pythonic way of checking the type.