Home > Mobile >  How to merge dictionaries in list according to specify condition using python code
How to merge dictionaries in list according to specify condition using python code

Time:02-15

I have some switches in the stack. There is only one IP of this stack, but there are several switches, so I need collect IP addr and all switch IDs of this stac in dict.

I have one list with dicts:

list = [
{'id': '107', 'name': sw1, 'ip': '10.10.10.5', 'stack_id': 'sw1', 'manufcturer': 'cisco'}, 
{'id': '352', 'name': sw2, 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'}, 
{'id': '125', 'name': sw3, 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'}, 
{'id': '90', 'name': sw4, 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'}, 
{'id': '148', 'name': sw5, 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'}, 
{'id': '45', 'name': sw6, 'ip': 'None', 'stack_id': 'sw6', 'manufcturer': 'cisco'} ]

I need compare key stack_id, if values of this keys are equal in this dicts, then need to create nested dicts like present bellow:

list2 = [
  {dev1 : {'ip': '10.10.10.5', 'id1': '107' 'id2': '352', 'id3': '125', 'id4': '90', 'id5': '148'}}
, {'id': '45', 'name': sw6, 'ip': 'None', 'stack_id': 'sw6', 'manufcturer': 'cisco'}
, ...
]

Please help. I'm a new with python and didn't find similar solution for this task. Thank you!

CodePudding user response:

below my proposal, and one small remark, never use the list keyword in your code, because it can break your code on unexpected places :)

l = [
{'id': '107', 'name': "sw1", 'ip': '10.10.10.5', 'stack_id': 'sw1', 'manufcturer': 'cisco'},
{'id': '352', 'name': "sw2", 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'},
{'id': '125', 'name': "sw3", 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'},
{'id': '90', 'name': "sw4", 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'},
{'id': '148', 'name': "sw5", 'ip': 'None', 'stack_id': 'sw1', 'manufcturer': 'cisco'},
{'id': '45', 'name': "sw6", 'ip': 'None', 'stack_id': 'sw6', 'manufcturer': 'cisco'} ]


from pprint import pprint
tmp = dict()
for i in l:
    tmp.setdefault(i["stack_id"],[])
    tmp[i["stack_id"]].append(i)

pprint(tmp)

Result:

{'sw1': [{'id': '107',
          'ip': '10.10.10.5',
          'manufcturer': 'cisco',
          'name': 'sw1',
          'stack_id': 'sw1'},
         {'id': '352',
          'ip': 'None',
          'manufcturer': 'cisco',
          'name': 'sw2',
          'stack_id': 'sw1'},
         {'id': '125',
          'ip': 'None',
          'manufcturer': 'cisco',
          'name': 'sw3',
          'stack_id': 'sw1'},
         {'id': '90',
          'ip': 'None',
          'manufcturer': 'cisco',
          'name': 'sw4',
          'stack_id': 'sw1'},
         {'id': '148',
          'ip': 'None',
          'manufcturer': 'cisco',
          'name': 'sw5',
          'stack_id': 'sw1'}],
 'sw6': [{'id': '45',
          'ip': 'None',
          'manufcturer': 'cisco',
          'name': 'sw6',
          'stack_id': 'sw6'}]}

  • Related