Question: Say I have the following list:

```
result = [{"name": "a", "number": 1},
{"name": "a", "number": 2},
{"name": "b", "number": 1},
{"name": "a", "number": 1}]

```

Can I turn it into something like:

```
result = [{"name": "a", "number": 1, "count": 2},
{"name": "a", "number": 2},
{"name": "b", "number": 1}]

```

I tried using the Count class but I couldn't make it work with dictionaries.

Answer: 
You can use `collections.Counter` with list comprehension:

```
from collections import Counter
[dict(tuple(t) + (('count', c),)) for t, c in Counter(frozenset(d.items()) for d in result).items()]

```

This returns:

```
[{'number': 1, 'name': 'a', 'count': 2}, {'number': 2, 'name': 'a', 'count': 1}, {'number': 1, 'name': 'b', 'count': 1}]

```
One way is to use `collections.Counter` to count dictionaries by `('name', 'number')`, then add counts conditional on the count being greater than 1. This can be achieved using a list comprehension:

```
from collections import Counter
from operator import itemgetter

keys = ('name', 'number')
c = Counter(map(itemgetter(*keys), L))

res = [{**dict(zip(keys, k)), **({'count': v} if v > 1 else {})} \
       for k, v in c.items()]

```

Result:

```
[{'count': 2, 'name': 'a', 'number': 1},
 {'name': 'a', 'number': 2},
 {'name': 'b', 'number': 1}]

```