Home > Net >  Return the difference of two dictionaries
Return the difference of two dictionaries

Time:03-31

Apologies for what will no doubt be a beginners question.

What I am trying to do is return the key, value pair difference between two dictionaries.

This simple bit of code does the trick:

remaining_macs = xlsx_macs.items() - xmc_macs.items()

But is returning a set as below, instead of the key, value dictionary pairs I would like?

{('70:B5:E8:C5:06:6B', 'Staff-Group'), ('34:9F:7B:56:53:C0', 'Printer-Group'), ('74:BF:C0:DE:7E:74', 'Printer-Group'), ('D4:3D:7E:BB:49:C4', 'Staff-Group'), ('8C:8C:AA:B5:1D:D4', 'Staff-Group')}

This is the original format of the dictionaries:

xlsx_macs:

{'8C:8C:AA:B5:1D:D4': 'Staff-Group', '70:B5:E8:C5:06:6B': 'Staff-Group', 'D4:3D:7E:BB:49:C4': 'Staff-Group', '34:9F:7B:56:06:23': 'Printer-Group', '34:9F:7B:56:53:C0': 'Printer-Group', '74:BF:C0:DE:7E:74': 'Printer-Group', 'BC:F3:10:E2:BF:40': 'Access Points', 'BC:F3:10:E2:C6:C0': 'Access Points', 'BC:F3:10:E2:CB:00': 'Access Points', '00:13:5E:8D:19:EE': 'VoIP Phones', '44:47:CC:DF:75:AB': 'CCTV-Group', '00:08:5D:75:D1:BB': 'Voice-Group', '00:08:5D:75:D1:A5': 'Voice-Group', '00:08:5D:57:17:30': 'Voice-Group', ...}

xmc_macs:

{'00:0A:83:02:35:8E': 'Salto-Group', '00:0A:83:02:3D:7D': 'Salto-Group', '00:0A:83:02:3D:AE': 'Salto-Group', '00:0A:83:02:40:C8': 'Salto-Group', '00:0A:83:02:40:C9': 'Salto-Group', '00:0A:83:02:40:D0': 'Salto-Group', '00:0A:83:02:40:DF': 'Salto-Group', '34:9F:7B:56:06:23': 'Printer-Group', 'BC:F3:10:E2:BF:40': 'Access Points', 'BC:F3:10:E2:C6:C0': 'Access Points', 'BC:F3:10:E2:CB:00': 'Access Points', 'BC:F3:10:E2:D1:00': 'Access Points', 'BC:F3:10:E2:D4:00': 'Access Points', 'BC:F3:10:E3:BD:40': 'Access Points', ...}

So I am struggling with the output that I want to remain as a dictionary, or finding some code that does the job better.

Many thanks in advance

CodePudding user response:

You can convert your result to a dictionary:

remaining_macs = dict(xlsx_macs.items() - xmc_macs.items())

Example:

>>> dict({('70:B5:E8:C5:06:6B', 'Staff-Group'), ('34:9F:7B:56:53:C0', 'Printer-Group'), ('74:BF:C0:DE:7E:74', 'Printer-Group'), ('D4:3D:7E:BB:49:C4', 'Staff-Group'), ('8C:8C:AA:B5:1D:D4', 'Staff-Group')})
{'74:BF:C0:DE:7E:74': 'Printer-Group', 'D4:3D:7E:BB:49:C4': 'Staff-Group', '70:B5:E8:C5:06:6B': 'Staff-Group', '8C:8C:AA:B5:1D:D4': 'Staff-Group', '34:9F:7B:56:53:C0': 'Printer-Group'}
  • Related