I have a list of objects.
My list format:
my_list = [
{
"y": "9",
"x": "num.nine"
},
{
"y": "8",
"x": "eight"
},
{
"y": "7",
"x": "num.seven"
},
{
"y": "6",
"x": "six"
},
{
"y": "5",
"x": "num.five"
}
]
I want to delete the objects where my_list['x'] is not like num.
This is what I did:
for val in my_list:
if('num' not in val['x']):
del val['x']
del val['y']
pprint(my_list)
The result of that code is:
[
{'x': 'num.nine', 'y': '9'},
{},
{'x': 'num.seven', 'y': '7'},
{},
{'x': 'num.five', 'y': '5'}
]
How do I delete the whole object?
My expected result is:
[
{'x': 'num.nine', 'y': '9'},
{'x': 'num.seven', 'y': '7'},
{'x': 'num.five', 'y': '5'}
]
my_list = [obj for obj in my_list if obj['x'].startswith('num.')]