# python – finding duplicates in a list of lists

## python – finding duplicates in a list of lists

Lots of good answers, but they all use rather more code than I would for this, so heres my take, for what its worth:

``````totals = {}
for k,v in original_list:
totals[k] = totals.get(k,0) + v

# totals = {a: 2, c: 2, b: 7}
``````

Once you have a dict like that, from any of these answers, you can use `items` to get a(n object that acts like a) list of tuples:

``````totals.items()
# => dict_items([(a, 2), (c, 2), (b, 7)])
``````

And run `list` across the tuples to get a list of lists:

``````[list(t) for t in totals.items()]
# => [[a, 2], [c, 2], [b, 7]]
``````

And sort if you want them in order:

``````sorted([list(t) for t in totals.items()])
# => [[a, 2], [b, 7], [c, 2]]

``````
``````>>> from collections import Counter
>>> lst = [[a, 1], [b, 1], [a, 1], [b, 1], [b, 2], [c, 2], [b, 3]]
>>> c = Counter(x for x, c in lst for _ in xrange(c))

Counter({b: 7, a: 2, c: 2})

>>> map(list, c.iteritems())
[[a, 2], [c, 2], [b, 7]]
``````

Or alternatively, without repeating each item `(a, b)` b times (@hcwhsa):

``````>>> from collections import Counter
>>> lst = [[a, 1], [b, 1], [a, 1], [b, 1], [b, 2], [c, 2], [b, 3]]
>>> c = sum((Counter(**{k:v}) for k, v in lst), Counter())

Counter({b: 7, a: 2, c: 2})

>>> map(list, c.iteritems())
[[a, 2], [c, 2], [b, 7]]
``````

## SOLUTION

Use `collections.Counter`:

``````from collections import Counter
original_list = [[a, 1], [b, 1], [a, 1], [b, 1], [b, 2], [c, 2], [b, 3]]
result = Counter()
for k, v in original_list:
result.update({k:v})

map(list, result.items())
# [[a, 2], [c, 2], [b, 7]]
``````

## FINDINGS

So, lot of answers, views and upvotes. I even earned my first `Nice answer` out of nothing (in last 2 days I made lot of answers worth of more research and efforts). In view of this, I decided to do at least some research and test solutions performance with a simple script written from scratch. Do not include code directly in answer for the sake of size.

Each function is named for its author an easily can be found in question. `thefourtheye`s solution now equals one of Mark Reed and is evaluated in original form, thefourtheye2 states for `itertools.groupby` based solution.

Each was tested several times (samples), each sample in turn invoked several function iterations. I evaluated min, max and standard deviation for samples times.

Here we go, running probing test for 10 times.

``````testing: thefourtheye, kroolik2, void, kroolik, alko, reed, visser
10 samples
10 iterations each
author   min     avg     max    stddev
reed 0.00000 0.00000 0.00000 0.00000
visser 0.00000 0.00150 0.01500 0.00450
thefourtheye 0.00000 0.00160 0.01600 0.00480
thefourtheye2 0.00000 0.00310 0.01600 0.00620
alko 0.00000 0.00630 0.01600 0.00772
void 0.01500 0.01540 0.01600 0.00049
kroolik2 0.04700 0.06430 0.07800 0.00831
kroolik 0.32800 0.34380 0.37500 0.01716
``````

Look at bottom two rows: at this point kroolik solutions were disqualified since with it any reasonable amount of samples*iterations will be performed for hours. Here goes final tests. I manually added number of upvotes to ouptut:

``````testing: thefourtheye, kroolik2, void, kroolik, alko, reed, visser
100 samples
1000 iterations each
author  upvotes   min     avg     max    stddev
reed      0.06200 0.08174 0.15600 0.01841
thefourtheye       0.06200 0.09971 0.20300 0.01911
visser       0.10900 0.12392 0.23500 0.02263
thefourtheye2          0.25000 0.29674 0.89000 0.07183
alko      0.56200 0.62309 1.04700 0.08438
void       1.50000 1.65480 2.39100 0.18721
kroolik       [DSQ]
``````