Question: I would like to figure out if any deal is selected twice or more.

The following example is stripped down for sake of readability. But in essence I thought the best solution would be using a dictionary, and whenever any deal-container (e.g. deal\_pot\_1) contains the same deal twice or more, I would capture it as an error.

The following code served me well, however by itself it throws an exception...

```
    if deal_pot_1:
       duplicates[deal_pot_1.pk] += 1

    if deal_pot_2:
        duplicates[deal_pot_2.pk] += 1

    if deal_pot_3:
        duplicates[deal_pot_3.pk] += 1

```

...if I didn't initialize this before hand like the following.

```
    if deal_pot_1:
       duplicates[deal_pot_1.pk] = 0

    if deal_pot_2:
        duplicates[deal_pot_2.pk] = 0

    if deal_pot_3:
        duplicates[deal_pot_3.pk] = 0

```

Is there anyway to simplify/combine this?

Answer: 
It looks like you want [`collections.Counter`](http://docs.python.org/library/collections.html#counter-objects).
So you only want to know if there are duplicated values? Then you could use a [set](http://docs.python.org/tutorial/datastructures.html#sets):

```
duplicates = set()
for value in values:
    if value in duplicates():
        raise Exception('Duplicate!')
    duplicates.add(value)

```

If you would like to find all duplicated:

```
maybe_duplicates = set()
confirmed_duplicates = set()

for value in values:
    if value in maybe_duplicates():
        confirmed_duplicates.add(value)
    else:
        maybe_duplicates.add(value)

if confirmed_duplicates:
    raise Exception('Duplicates: ' + ', '.join(map(str, confirmed_duplicates)))

```