3

I have a list like this.

data = [{
'category': 'software', 
'code': 110, 
'actual': '["5.1.4"]', 
'opened': '2018-10-16T09:18:12Z', 
'component_type': 'update', 
'event': 'new update available', 
'current_severity': 'info', 
'details': '', 
'expected': None, 
'id': 10088862, 
'component_name': 'Purity//FA'
}, 
{
'category': 'software', 
'code': 67, 
'actual': None, 
'opened': '2018-10-18T01:14:45Z', 
'component_type': 'host', 
'event': 'misconfiguration', 
'current_severity': 'critical', 
'details': '', 
'expected': None, 
'id': 10088898, 
'component_name': 'pudc-vm-001'
}, 
{
'category': 'array', 
'code': 42, 
'actual': None, 
'opened': '2018-11-22T22:27:29Z', 
'component_type': 'hardware', 
'event': 'failure', 
'current_severity': 'warning', 
'details': ''  , 
'expected': None, 
'id': 10089121, 
'component_name': 'ct1.eth15'
}]

I want to iterate over this and get only category, component_type, event and current_severity.

I tried a for loop but it says too values to unpack, obviously.

for k, v, b, n in data:
                print(k, v, b, n) //do something

i essentially want a list that is filtered to have only category, component_type, event and current_severity. So that i can use the same for loop to get out my four key value pairs.

Or if there is a better way to do it? Please help me out.

Note: The stanzas in the list is not fixed, it keeps changing, it might have more than three stanzas.

Sanjeev Siva
  • 930
  • 1
  • 9
  • 25
Bharath
  • 559
  • 11
  • 27

3 Answers3

3

If you know that every dict inside your current list of dicts should have at least the keys you're trying to extract their data, then you can use dict[key], however for safety, i prefer using dict.get(key, default value) like this example:

out = [
    {
        'category': elm.get('category'),
        'component_type': elm.get('component_type'),
        'event': elm.get('event'),
        'current_severity': elm.get('current_severity')
    } for elm in data
]
print(out)

Output:

[{'category': 'software',
  'component_type': 'update',
  'current_severity': 'info',
  'event': 'new update available'},
 {'category': 'software',
  'component_type': 'host',
  'current_severity': 'critical',
  'event': 'misconfiguration'},
 {'category': 'array',
  'component_type': 'hardware',
  'current_severity': 'warning',
  'event': 'failure'}]

For more informations about when we should use dict.get() instead of dict[key], see this answer

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
  • 1
    @Bharath Yes indeed. Look at the link i've provided in the answer and [this one](https://docs.python.org/3/library/stdtypes.html#dict.get) too from the official documentation. – Chiheb Nexus Feb 07 '19 at 02:04
3

You have a list of dictionaries, simple way to iterate over this is

category = [x['category'] for x in data]

Which prints the values of category key

['software', 'software', 'array']

Do the same for component_type, event and current_severity and you're good to go

Sanjeev Siva
  • 930
  • 1
  • 9
  • 25
2

with this you get a new list with only the keys you are interested on:

new_list = [{
    'category': stanza['category'],
    'component_type': stanza['component_type'],
    'event': stanza['event'],
 } for stanza in data]
HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93