I'm looking for a clean way to check if an item is member of a list of objects. Basically this is what I want to do:
class Constant: def __init__(self,name,value): self.name = name self.value = value list = [Constant('el1',1),Constant('el2',2)] list2= ['el4','el5','el1'] for item in list2: #clean solution for this if clause is needed (I'm aware list.name triggers an error) if item in list.name: print 'it is a member' So it is important to me that the item matches only on the name, the value has no meaning when searching. I know I can solve this by adding an additional for loop like this:
for item in list2: for itemConstant in list: if item == itemConstant.name: print 'it is a member' But I want to be sure there is no better solution than this.