Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two keys in dictionary hold the same value

This seems like such an obvious thing that I feel like I'm missing out on something, but how do you find out if two different keys in the same dictionary have the exact same value? For example, if you have the dictionary test with the keys a, b, and c and the keys a and b both have the value of 10, how would you figure that out? (For the point of the question, please assume a large number of keys, say 100, and you have no knowledge of how many duplicates there are, if there are multiple sets of duplicates, or if there are duplicates at all). Thanks.

like image 475
Kpower Avatar asked Jul 23 '13 21:07

Kpower


People also ask

Can 2 keys have same value in dictionary?

No, each key in a dictionary should be unique. You can't have two keys with the same value.

How do you compare two keys in a dictionary?

The compare method cmp() is used in Python to compare values and keys of two dictionaries. If method returns 0 if both dictionaries are equal, 1 if dic1 > dict2 and -1 if dict1 < dict2.

How do you check if two values are the same in a dictionary python?

Using == operator to Compare Two Dictionaries Here we are using the equality comparison operator in Python to compare two dictionaries whether both have the same key value pairs or not.


3 Answers

len(dictionary.values()) == len(set(dictionary.values()))

This is under the assumption that the only thing you want to know is if there are any duplicate values, not which values are duplicates, which is what I assumed from your question. Let me know if I misinterpreted the question.

Basically this is just checking if any entries were removed when the values of the dictionary were casted to an object that by definition doesn't have any duplicates.

If the above doesn't work for your purposes, this should be a better solution:

set(k for k,v in d.items() if d.values().count(v) > 1))

Basically the second version just checks to see if there is more than one entry that will be removed if you try popping it out of the list.

like image 135
Slater Victoroff Avatar answered Oct 13 '22 12:10

Slater Victoroff


keys = set()
for key1 in d:
    for key2 in d:
        if key1 == key2: continue
        if d[key1] == d[key2]:
            keys |= {key1, key2}

i.e. that's Θ(n²) what you want. The reason is that a dict does not provide Θ(1) search of a key, given a value. So better rethink your data structure choices if that's not good enough.

like image 39
hdante Avatar answered Oct 13 '22 14:10

hdante


You can use list in conjunction with dictionary to find duplicate elements! Here is a simple code demonstrating the same:

d={"val1":4,"val2":4,"val3":5,"val4":3}
l=[]
for key in d:
      l.append(d[key])
      l.sort()
print(l)
for i in range(len(l)):
    if l[i]==l[i+1]:
        print("true, there are duplicate elements.")
        print("the keys having duplicate elements are: ")
        for key in d:
            if d[key]==l[i]:
                print(key)
        break

output:

runfile('C:/Users/Andromeda/listeqtest.py', wdir='C:/Users/Andromeda')

[3, 4, 4, 5]
true, there are duplicate elements.
the keys having duplicate elements are: 
val1
val2

when you sort the elements in the list, you will find that equal values always appear together!

like image 20
anirudh1998 Avatar answered Oct 13 '22 12:10

anirudh1998