I have a json list like this:
[
{
"id": "1",
"name": "ramesh",
"phone": "12345",
"salary": "50000"
},
{
"id": "2",
"name": "suresh",
"phone": "123456",
"salary": "60000"
},
{
"id": "3",
"name": "mahesh",
"phone": "123457",
"salary": "70000"
}
]
How can I retrieve and show only salary
column if the salary
is above 60000? I tried to convert my output to python dict but not really sure how can i filter it.
Expected Output:
[
{
"salary": "60000"
},
{
"salary": "70000"
}
]
CodePudding user response:
If you have a python list of dicts already, this would be your solution:
[{'salary': i['salary']} for i in x if int(i['salary']) >= 60000] # x is your list of dicts
Output:
[{'salary': '60000'}, {'salary': '70000'}]
CodePudding user response:
You could try the walrus operator (:=
) as of Python 3.8:
>>> [{'salary': x} for dct in lst if int(x := dct['salary']) >= 60000]
[{'salary': '60000'}, {'salary': '70000'}]
>>>
For version under 3.8:
>>> [{'salary': dct['salary']} for dct in lst if int(dct['salary']) >= 60000]
[{'salary': '60000'}, {'salary': '70000'}]
>>>