Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to select a list with a specific value

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading