Checking if type == list in python
You should try using
if isinstance(object, list): ## DO what you want
In your case
if isinstance(tmpDict[key], list): ## DO SOMETHING
x = [1,2,3]if type(x) == list(): print "This wont work"if type(x) == list: ## one of the way to see if it's list print "this will work" if type(x) == type(list()): print "lets see if this works"if isinstance(x, list): ## most preferred way to check if it's list print "This should work just fine"
The difference between
type() though both seems to do the same job is that
isinstance() checks for subclasses in addition, while
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.