If I had the following code
attributes = []
attributes.append({'attribute': 'noir', 'group': 'coloris', 'id': '8'})
attributes.append({'attribute': 's', 'group': 'taille_textile', 'id': '29'})
attributes.append({'attribute': 'm', 'group': 'taille_textile', 'id': '24'})
attributes.append({'attribute': 'l', 'group': 'taille_textile', 'id': '25'})
attributes.append({'attribute': 'xl', 'group': 'taille_textile', 'id': '26'})
and I wanted to return an object of the list which contained a certain id, what would be the best way to do that?
I know that one solution would be to use the for
loop like this
def getItemById(id):
for i in attributes:
for k,v in i.items():
if (k == 'id' and v == id):
return i
I'm sure there must be a much more elegant or efficient way to do it other than this?
Is there an opportunity to use lambdas here? would that give a performance benefit?