Best way to aggregate a simple dict in Python

user650108

My problem is simple: I have a list of dicts, and I want to make a count aggregation of this into a new dictionary, on a particular key. Just like this, but in Python.

Original data

mylist = [
    {'date': '16-01-2016', 'name': 'A'},
    {'date': '16-01-2016', 'name': 'B'},
    {'date': '17-01-2016', 'name': 'C'},
    {'date': '17-01-2016', 'name': 'D'},
    {'date': '17-01-2016', 'name': 'E'},
    {'date': '18-01-2016', 'name': 'F'},
]

Result

{'17-01-2016': 3, '16-01-2016': 2, '18-01-2016': 1}

How to?

What is the best solution to achieve this? I would like to do it a more beautiful and pythonic way than that ugly piece of code:

result = {}
for item in mylist:
    if not item['date'] in result:
        result[item['date']] = 1
    else:
        result[item['date']] += 1

Many thanks for considering my request!

Padraic Cunningham

You can use a collections.Counter dict with map and operator.itemgetter:

from collections import Counter
from operator import itemgetter

mylist = [
    {'date': '16-01-2016', 'name': 'A'},
    {'date': '16-01-2016', 'name': 'B'},
    {'date': '17-01-2016', 'name': 'C'},
    {'date': '17-01-2016', 'name': 'D'},
    {'date': '17-01-2016', 'name': 'E'},
    {'date': '18-01-2016', 'name': 'F'},
]

counts = Counter(map(itemgetter("date"), mylist))

Output:

Counter({'17-01-2016': 3, '16-01-2016': 2, '18-01-2016': 1})

Or using just a gen exp:

counts = Counter(d["date"] for d in mylist)

If you are using python2, use itertools.imap in place of map.

On a side note, if item['date'] not in result reads better than if not item['date'] in result.

If speed really matters, the map and itemgetter will be a little faster:

In [16]: timeit  Counter(map(itemgetter("date"), mylist))
10 loops, best of 3: 23.9 ms per loop

In [17]: timeit  Counter(d["date"] for d in mylist)
10 loops, best of 3: 26.8 ms per loop

In [18]: timeit Counter(map(lambda x: x['date'], mylist))
10 loops, best of 3: 34.9 ms per loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Best way to aggregate a simple dict in Python

From Dev

Best way for dict with duplicate keys in python

From Dev

simple and best way of list manipulation in python

From Dev

What’s the best way to Convert a list to dict in Python2.7

From Dev

Best way to aggregate logging data

From Dev

How to change value for item in a list of dict in python in a simple way?

From Dev

How to change value for item in a list of dict in python in a simple way?

From Dev

Best way to handle a keyerror in a dict

From Dev

Best way to concatenate column values in aggregate

From Dev

What is the best way to index aggregate data on ElasticSearch

From Dev

What is the best way to append lists inside a dict?

From Dev

Python efficient way to filter dict

From Dev

Best Way to Format a Simple When Function in JS

From Dev

JavaFX: what is the best way to display a simple message?

From Dev

Best way to determine if simple or extended MAPI was loaded

From Dev

What is the best way to aggregate Streams into one DISTINCT with Java 8

From Dev

Flutter - Best way to aggregate data from child widgets in an IndexedStack

From Dev

Best implementation of dual-side dict in Python

From Dev

Best way to extract data from dict with multiple dicts and lists?

From Dev

Any simple python package providing dict merge?

From Dev

Best way to validate a name in Python

From Dev

best way for python to interface with MySQL

From Dev

Best way to "overload" function in python?

From Dev

Best way to multiprocessing python on windows

From Dev

Best way to validate a name in Python

From Dev

Best way to call a method - Python

From Dev

Best way to quit a python programme?

From Dev

Python safe dict navigation, The Right Way

From Dev

Python dict get value fastest way

Related Related

  1. 1

    Best way to aggregate a simple dict in Python

  2. 2

    Best way for dict with duplicate keys in python

  3. 3

    simple and best way of list manipulation in python

  4. 4

    What’s the best way to Convert a list to dict in Python2.7

  5. 5

    Best way to aggregate logging data

  6. 6

    How to change value for item in a list of dict in python in a simple way?

  7. 7

    How to change value for item in a list of dict in python in a simple way?

  8. 8

    Best way to handle a keyerror in a dict

  9. 9

    Best way to concatenate column values in aggregate

  10. 10

    What is the best way to index aggregate data on ElasticSearch

  11. 11

    What is the best way to append lists inside a dict?

  12. 12

    Python efficient way to filter dict

  13. 13

    Best Way to Format a Simple When Function in JS

  14. 14

    JavaFX: what is the best way to display a simple message?

  15. 15

    Best way to determine if simple or extended MAPI was loaded

  16. 16

    What is the best way to aggregate Streams into one DISTINCT with Java 8

  17. 17

    Flutter - Best way to aggregate data from child widgets in an IndexedStack

  18. 18

    Best implementation of dual-side dict in Python

  19. 19

    Best way to extract data from dict with multiple dicts and lists?

  20. 20

    Any simple python package providing dict merge?

  21. 21

    Best way to validate a name in Python

  22. 22

    best way for python to interface with MySQL

  23. 23

    Best way to "overload" function in python?

  24. 24

    Best way to multiprocessing python on windows

  25. 25

    Best way to validate a name in Python

  26. 26

    Best way to call a method - Python

  27. 27

    Best way to quit a python programme?

  28. 28

    Python safe dict navigation, The Right Way

  29. 29

    Python dict get value fastest way

HotTag

Archive