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

前端 未结 1 963
灰色年华
灰色年华 2020-12-13 01:47

I want to check if my list of objects contain an object with a certain attribute value.

class Test:
    def __init__(self, name):
        self.name = name

         


        
相关标签:
1条回答
  • 2020-12-13 02:40

    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)
    
    0 讨论(0)
提交回复
热议问题