Have a list called summary
containing a corresponding JSON object similar to:
JavaScript
x
14
14
1
{
2
# summary[0]
3
"object1": {
4
"json_data": "{json data information}",
5
"tables_data": "TABLES_DATA"
6
},
7
8
# summary[1]
9
"object2": {
10
"json_data": "{json data information}",
11
"tables_data": ""
12
}
13
}
14
Essentially, if "tables_data": ""
is found within the string of a list item, I want the entire list item to be removed.
How would I go about doing this?
Advertisement
Answer
You can do a dictionary-comprehension selecting the dictionary item with 'tables_data'
has a value not equals ''
:
JavaScript
1
16
16
1
summary = [{
2
# summary[0]
3
"object1": {
4
"json_data": "{json data information}",
5
"tables_data": "TABLES_DATA"
6
},
7
8
# summary[1]
9
"object2": {
10
"json_data": "{json data information}",
11
"tables_data": ""
12
}
13
}]
14
result = [{k: d for k, d in s.items() if d.get('tables_data') != ''} for s in summary]
15
# [{'object1': {'json_data': '{json data information}', 'tables_data': 'TABLES_DATA'}}]
16