What is the fastest way I can check for a certain types existence in a list?
I wish I could 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 within the python 2.7 world. But 3.0 solutions will be ok!
Use isinstance() to check if an object has type list. Call isinstance(object, class_or_tuple) with class_or_tuple as list to return True if object is an instance or subclass of list and False if otherwise.
if (myList. Contains(myString)) string element = myList. ElementAt(myList. IndexOf(myString));
To check if the item exists in the list, use Python “in operator”. For example, we can use the “in” operator with the if condition, and if the item exists in the list, then the condition returns True, and if not, then it returns False.
if any(isinstance(x, SubclassOne) for x in list_of_stuff):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With