Check if list contains type?

What is the fastest way to check for the existence of certain types in a list?

I would like to do the following:

class Generic(object) ... def ... class SubclassOne(Generic) ... def ... class SubclassOne(Generic) ... def ... thing_one = SubclassOne() thing_two = SubclassTwo() list_of_stuff = [thing_one, thing_two] if list_of_stuff.__contains__(SubclassOne): print "Yippie!" 

EDIT: Trying to stay in the python 2.7 world. But 3.0 solutions will be fine!

+6
source share
2 answers

if any(isinstance(x, SubclassOne) for x in list_of_stuff):

+11
source

You can use any and isinstance .

 if any(isinstance(item, SubClassOne) for item in list_of_stuff): print "Yippie!" 
+2
source

All Articles