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

find extra key in dict by value

I have to dicts like this

new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}

old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}

values in them are the same, just new_ids have 9 of them and old_ids have 10. how is to find this extra key that exists in new_ids?

value of 5537179 in old_ids does not exist in new_ids, so i need to get a key 10722773

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

>Solution :

First way

_1. only key


new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}

old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}

notLst = [k for k,v in old_ids.items() if v not in new_ids.values()]

print(notLst)

OUTPUT

[10722773]
_2. both key and value

new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}

old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}

notLst = {k:v for k,v in old_ids.items() if v not in new_ids.values()}

print(notLst)

Output

{10722773: 5537179}

There are more ways too. Fastest way also

Second Way

You can use a set way too to solve the problem

new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}

old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}


n = set.symmetric_difference(set(new_ids.values()),set(old_ids.values()))

notLst = {k:v for k,v in old_ids.items() if v in n} # both key Value
# notLst = [k for k,v in old_ids.items() if v in n] # Only key

print(notLst)

Output

{10722773: 5537179} # for both key value
[10722773] # for only key

Time difference between my second way and the @AKK way

from timeit import timeit

new_ids = {10722774: 5537170, 10722775: 5537171, 10722776: 5537172, 10722777: 5537173, 10722778: 5537174, 10722779: 5537175, 10722780: 5537176, 10722781: 5537177, 10722782: 5537178}

old_ids = {10722773: 5537179, 10722764: 5537170, 10722765: 5537171, 10722766: 5537172, 10722767: 5537173, 10722768: 5537174, 10722769: 5537175, 10722770: 5537176, 10722771: 5537177, 10722772: 5537178}

def myway():
    n = set.symmetric_difference(set(new_ids.values()),set(old_ids.values()))

    notLst = {k:v for k,v in old_ids.items() if v in n} # both key Value
    # notLst = [k for k,v in old_ids.items() if v in n] # Only key

    return notLst


def AKKway():

    flipped_old = {v: k for k, v in old_ids.items()}
    flipped_new = {v: k for k, v in new_ids.items()}
    flipped_combined = flipped_old | flipped_new
    # Find keys that aren't shared between the flipped dicts
    uncommon_keys = set(flipped_old) ^ set(flipped_new)
    # Generate a dict that unflips the key-values containing only the uncommon keys
    uncommon = {flipped_combined[v]: v for v in uncommon_keys}
    return (uncommon)


print("My way",timeit(myway,number=10000))
print("Akk way",timeit(AKKway,number=10000))

OUTPUT

My way 0.010995300021022558
Akk way 0.028010500012896955
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