data = [
{
"2022": [
{
"Title": "Title"
},
{
"Title": "Title 2"
}
]
},
{
"2023": []
},
{
"2024": []
}
]
Now I want to remove 2023 and 2024 because it do not have any value. Solution in python:
for el in data:
for dict in el.values():
if len(dict) == 0:
data.remove(el)
Similar Solution in JS:
data = data.filter(item=>Object.values(item)[0].length > 0)
What i want is similar to js. (I am not an expert in python)
Solution in one line:
data = [
{
"2022": [
{
"Title": "Title"
},
{
"Title": "Title 2"
}
]
},
{
"2023": []
},
{
"2024": []
}
]
result = [{k: v} for el in data for k, v in el.items() if len(v) > 0]
Output:
[{'2022': [{'Title': 'Title'}, {'Title': 'Title 2'}]}]
You can use this:
data = [
{
"2022": [
{
"Title": "Title"
},
{
"Title": "Title 2"
}
]
},
{
"2023": []
},
{
"2024": []
}
]
data = [k for k in data for key in k.keys() if k[key]!=[]]
Output:
[{'2022': [{'Title': 'Title'}, {'Title': 'Title 2'}]}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.