Neemaximo
Neemaximo

Reputation: 20811

Duplicates in a dictionary (Python)

I need to write a function that returns true if the dictionary has duplicates in it. So pretty much if anything appears in the dictionary more than once, it will return true.

Here is what I have but I am very far off and not sure what to do.

d = {"a", "b", "c"}

def has_duplicates(d):
    seen = set()
    d={}
    for x in d:
        if x in seen:
            return True
        seen.add(x)
    return False

print has_duplicates(d)

Upvotes: 1

Views: 11005

Answers (7)

César
César

Reputation: 10119

This is not a dictionary, is a set:

d = {"a", "b", "c"}

I don't know what are you trying to accomplish but you can't have dictionaries with same key. If you have:

>>> d = {'a': 0, 'b':1}
>>> d['a'] = 2
>>> print d
{'a': 2, 'b': 1}

Upvotes: 0

Michael Dillon
Michael Dillon

Reputation: 32392

The only thing that a dictionary can have duplicates of, is values. A dictionary is a key, value store where the keys are unique. In Python, you can create a dictionary like so:

d1 = {k1: v1, k2: v2, k3: v1}
d2 = [k1, v1, k2, v2, k3, v1]

d1 was created using the normal dictionary notation. d2 was created from a list with an even number of elements. Note that both versions have a duplicate value.

If you had a function that returned the number of unique values in a dictionary then you could say something like:

len(d1) != func(d1)

Fortunately, Python makes it easy to do this using sets. Simply converting d1 into a set is not sufficient. Lets make our keys and values real so you can run some code.

v1 = 1; v2 = 2
k1 = "a"; k2 = "b"; k3 = "c"
d1 = {k1: v1, k2: v2, k3: v1}
print len(d1)
s = set(d1)
print s

You will notice that s has three members too and looks like set(['c', 'b', 'a']). That's because a simple conversion only uses the keys in the dict. You want to use the values like so:

s = set(d1.values())
print s

As you can see there are only two elements because the value 1 occurs two times. One way of looking at a set is that it is a list with no duplicate elements. That's what print sees when it prints out a set as a bracketed list. Another way to look at it is as a dict with no values. Like many data processing activities you need to start by selecting the data that you are interested in, and then manipulating it. Start by selecting the values from the dict, then create a set, then count and compare.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363547

def has_duplicates(d):
    return False

Dictionaries do not contain duplicate keys, ever. Your function, btw., is equivalent to this definition, so it's correct (just a tad long).

If you want to find duplicate values, that's

len(set(d.values())) != len(d)

assuming the values are hashable.

Upvotes: 7

Raymond Hettinger
Raymond Hettinger

Reputation: 226256

Python dictionaries already have unique keys.

Are you possibly interested in unique values?

set(d.values())

If so, you can check the length of that set to see if it is smaller than the number of values. This works because sets eliminate duplicates from the input, so if the result is smaller than the input, it means some duplicates were found and eliminated.

Upvotes: 1

John Machin
John Machin

Reputation: 82924

Not only is your general proposition that dictionaries can have duplicate keys false, but also your implementation is gravely flawed: d={} means that you have lost sight of your input d arg and are processing an empty dictionary!

Upvotes: 0

Adam Wagner
Adam Wagner

Reputation: 16107

If you are looking to find duplication in values of the dictionary:

def has_duplicates(d):
    return len(d) != len(set(d.values()))

print has_duplicates({'a': 1, 'b': 1, 'c': 2})

Outputs:

True

Upvotes: 7

ᅠᅠᅠ
ᅠᅠᅠ

Reputation: 66940

In your code, d = {"a", "b", "c"}, d is a set, not a dictionary.

Neither dictionary keys nor sets can contain duplicates. If you're looking for duplicate values, check if the set of the values has the same size as the dictionary itself:

def has_duplicate_values(d):
    return len(set(d.values())) != len(d)

Upvotes: 3

Related Questions