Hey I have a list with this structure:
list = {{'type' = 102, color = 'blue'}, {'type' = 103, color = 'green'}}
My question is: how to filter elements by a specific number of type, for example if I want to get output like this:
{'type' = 102, color = 'blue'}
I want to filter it by an specific value of type (in this example 102). How can I do that?. Thank you!
>Solution :
Using filter():
data = [{'type': 102, 'color': 'blue'}, {'type': 103, 'color': 'green'}]
output = list(filter(lambda x: x['type'] == 102, data))
print(output)
Using list comprehension:
data = [{'type': 102, 'color': 'blue'}, {'type': 103, 'color': 'green'}]
output = [x for x in data if x["type"] == 102]
print(output)