'How to filter a dictionary by any key and return all keys-values attributes?
I have a dictionary whose keys and values are strings. I would like to filter the dictionary by some key = value and I would get as result all information about that filter.
For example:
I have a dictionary of data, say:
data =
{
"events":
[
{
"start_date": {
"year": "2007",
"month": "03"
},
"end_date": {
"year": "2008",
"month": "09"
},
"media": {
"url": "https://someurl_001.com",
"credit": "Some Credit 001"
},
"text": {
"headline": "Some Headline 001",
"text": "Some Text 001"
},
"tags": {
"tag1": "orange",
"tag2": "banana"
}
},
{
"start_date": {
"year": "2007",
"month": "06"
},
"end_date": {
"year": "2008",
"month": "02"
},
"media": {
"url": "https://someurl_002.com"
},
"text": {
"headline": "Some Headline 002",
"text": "Some Text 002"
},
"tags": {
"tag1": "orange",
"tag2": "lemon"
}
},
]
}
I wold like do something like:
some_function("year" = "2007", "month" = "03")
Output: Only dict that has this filter:
"events": [
{
"start_date": {
"year": "2007",
"month": "03"
},
"end_date": {
"year": "2008",
"month": "09"
},
"media": {
"url": "https://someurl_001.com",
"credit": "Some Credit 001"
},
"text": {
"headline": "Some Headline 001",
"text": "Some Text 001"
},
"tags": {
"tag1": "orange",
"tag2": "banana"
}
}
]
or
some_function("tag1" = "orange")
OUTPUT:
All dict because "tag1" : "orange" was in all my sample.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
