Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if list of objects contain an object with a certain attribute value

People also ask

How do you check if a list of object contains a value?

You can use the Enumerable. Where extension method: var matches = myList. Where(p => p.Name == nameToExtract);

How do you check if an object has a specific attribute?

We can use hasattr() function to find if a python object obj has a certain attribute or property. hasattr(obj, 'attribute'): The convention in python is that, if the property is likely to be there, simply call it and catch it with a try/except block.

How do you check if an object is in a list of objects Python?

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.


As you can easily see from the documentation, the any() function short-circuits an returns True as soon as a match has been found.

any(x.name == "t2" for x in l)