Reputation: 13
I need to convert this data structure into multiple dictionaries.
I have a datastructure, I want to convert it into dictionaries to make the script work.
p= {u'data': [{u'_id': u'5cd514f5b52af58fc58df832',
u'encap': u'vlan-18',
u'hostname': u'CGST',
u'ip': u'10.0.0.7',
u'mac': u'00:10:46:D6:00:40'},
{u'_id': u'5cd514f5b52af58fc58df830',
u'encap': u'vlan-15',
u'hostname': u'GBTW',
u'ip': u'10.0.4.1',
u'mac': u'00:40:39:B6:F8:3A'}]}
Can "mac" be the key and the rest be the item with 1 dictionary each. Something like :
{00:10:46:D6:00:40:{ip: '10.0.0.7',encap:'vlan-18',hostname:'CGST'}}
{00:40:39:B6:F8:3A:{ip: '10.0.4.1',encap:'vlan-15',hostname:'GBTW'}}
Upvotes: 0
Views: 36
Reputation: 82765
Using a dict comprehension
Ex:
p= {u'data': [{u'_id': u'5cd514f5b52af58fc58df832',
u'encap': u'vlan-18',
u'hostname': u'CGST',
u'ip': u'10.0.0.7',
u'mac': u'00:10:46:D6:00:40'},
{u'_id': u'5cd514f5b52af58fc58df830',
u'encap': u'vlan-15',
u'hostname': u'GBTW',
u'ip': u'10.0.4.1',
u'mac': u'00:40:39:B6:F8:3A'}]}
print({i.pop("mac"): i for i in p["data"]})
Output:
{u'00:10:46:D6:00:40': {u'_id': u'5cd514f5b52af58fc58df832',
u'encap': u'vlan-18',
u'hostname': u'CGST',
u'ip': u'10.0.0.7'},
u'00:40:39:B6:F8:3A': {u'_id': u'5cd514f5b52af58fc58df830',
u'encap': u'vlan-15',
u'hostname': u'GBTW',
u'ip': u'10.0.4.1'}}
Upvotes: 2