I have python dictionary with the below items
`
> ```
> {'HostName': 'DEMOBDDBX00100.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOBDDBX00200.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOBDDBX10101.demo', 'BackupStatus': 'FAILURE'}
> {'HostName': 'DEMOBDDBX10102.demo', 'BackupStatus': 'FAILURE'}
> {'HostName': 'DEMOBDDBX10201.demo', 'BackupStatus': 'FAILURE'}
> {'HostName': 'DEMOBDDBX10202.demo', 'BackupStatus': 'FAILURE'}
> {'HostName': 'DEMOBDMBX00100.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOBDMBX00200.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOBDMBX10101.demo', 'BackupStatus': 'FAILURE'}
> {'HostName': 'DEMOBDMBX10102.demo', 'BackupStatus': 'FAILURE'}
> {'HostName': 'DEMODACRT10100.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMODACRT10200.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMODACTS10101.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMODACTS10102.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOKLIRT10100.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOKLIRT10200.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOKNORT10100.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOKNORT10200.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOKOSRT10200.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOLABTS10300.demo', 'BackupStatus': 'SUCCESS'}
> {'HostName': 'DEMOLABTS10400.demo', 'BackupStatus': 'SUCCESS'}
> ```
I need to filter out the values in Hostname only if the BackupStatus == "FAILURE" I need the output as:
{'HostName': 'DEMOBDMBX10101.demo', 'BackupStatus': 'FAILURE'}
{'HostName': 'DEMOBDDBX10101.demo', 'BackupStatus': 'FAILURE'}
{'HostName': 'DEMOBDDBX10102.demo', 'BackupStatus': 'FAILURE'}
{'HostName': 'DEMOBDDBX10201.demo', 'BackupStatus': 'FAILURE'}
{'HostName': 'DEMOBDDBX10202.demo', 'BackupStatus': 'FAILURE'}`
```
Can someone please help me with this?
CodePudding user response:
This does not look like a dictionary but a list of dictionaries.
If that's the case, it seems you want to obtain a sub-list with only those elements (dictionaries) that have BackupStatus
equal to FAILURE
. So you could do something like this:
recs = [
{'HostName': 'DEMOBDDBX00100.demo', 'BackupStatus': 'SUCCESS'},
{'HostName': 'DEMOBDDBX00200.demo', 'BackupStatus': 'SUCCESS'},
{'HostName': 'DEMOBDDBX10101.demo', 'BackupStatus': 'FAILURE'},
...
]
failed_recs = [v for v in recs if v['BackupStatus'] == 'FAILURE']
CodePudding user response:
Consider utilizing a list comprehension:
>>> data = [
... {'HostName': 'DEMOBDDBX00100.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOBDDBX00200.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOBDDBX10101.demo', 'BackupStatus': 'FAILURE'},
... {'HostName': 'DEMOBDDBX10102.demo', 'BackupStatus': 'FAILURE'},
... {'HostName': 'DEMOBDDBX10201.demo', 'BackupStatus': 'FAILURE'},
... {'HostName': 'DEMOBDDBX10202.demo', 'BackupStatus': 'FAILURE'},
... {'HostName': 'DEMOBDMBX00100.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOBDMBX00200.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOBDMBX10101.demo', 'BackupStatus': 'FAILURE'},
... {'HostName': 'DEMOBDMBX10102.demo', 'BackupStatus': 'FAILURE'},
... {'HostName': 'DEMODACRT10100.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMODACRT10200.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMODACTS10101.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMODACTS10102.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOKLIRT10100.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOKLIRT10200.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOKNORT10100.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOKNORT10200.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOKOSRT10200.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOLABTS10300.demo', 'BackupStatus': 'SUCCESS'},
... {'HostName': 'DEMOLABTS10400.demo', 'BackupStatus': 'SUCCESS'},
... ]
>>> [d for d in data if d['BackupStatus'] == 'FAILURE']
[{'HostName': 'DEMOBDDBX10101.demo', 'BackupStatus': 'FAILURE'},
{'HostName': 'DEMOBDDBX10102.demo', 'BackupStatus': 'FAILURE'},
{'HostName': 'DEMOBDDBX10201.demo', 'BackupStatus': 'FAILURE'},
{'HostName': 'DEMOBDDBX10202.demo', 'BackupStatus': 'FAILURE'},
{'HostName': 'DEMOBDMBX10101.demo', 'BackupStatus': 'FAILURE'},
{'HostName': 'DEMOBDMBX10102.demo', 'BackupStatus': 'FAILURE'}]