Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return the difference of two dictionaries

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading