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

Search in List of dict Python

I have a list with some dicts inside. The list looks like this:

files = [
{'name': "text.txt", 'size': 10, 'location': "cloud", 'info': "Nothing"},
{'name': "img.jpg", 'size': 200, 'location': "local", 'info': "private"},
{'name': "cars.txt", 'size': 109, 'location': "cloud", 'info': "private"}
]

I also have a folder where "text.txt", "img.jpg" and "cars.txt" are located. Somehow I have to get all elements where "location" is "cloud" AND "info" is "Nothing".

The only way to sort the data is by using the "next" function

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

next((item for item in files if item["location"] == "cloud"), None)

What is the fastest way to achieve this? And also to sort by "location" and "info"?

>Solution :

One of the way will be this

list(filter(lambda x: x["location"]=="cloud" and x["info"]=="Nothing",files))
# [{'name': 'text.txt', 'size': 10, 'location': 'cloud', 'info': 'Nothing'}]

If you just want the names:

list(map(lambda x: x["name"], filter(lambda x: x["location"]=="cloud" and x["info"]=="Nothing",files)))
# ['text.txt']
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