You can use collection.Counter dict with queries that become a few short lines of code:
import requests from collections import Counter url = "http://data.police.uk/api/crimes-street/all-crime?lat=53.396246&lng=-2.646960&date=2013-03" json_obj = requests.get(url).json() c = Counter(player['category'] for player in json_obj) print(c)
Output:
Counter({'anti-social-behaviour': 79, 'criminal-damage-arson': 12, 'other-crime': 11, 'violent-crime': 9, 'vehicle-crime': 7, 'other-theft': 6, 'burglary': 4, 'public-disorder-weapons': 3, 'shoplifting': 2, 'drugs': 2})
If you prefer to have a normal voice recorder, then just call dict on the dict counter:
from pprint import pprint as pp c = dict(c) pp(c)
{'anti-social-behaviour': 79, 'burglary': 4, 'criminal-damage-arson': 12, 'drugs': 2, 'other-crime': 11, 'other-theft': 6, 'public-disorder-weapons': 3, 'shoplifting': 2, 'vehicle-crime': 7, 'violent-crime': 9}
Then you just access the c['drugs'] key, etc.
Or iterate over the elements for printing crime and counting in the desired format:
for k, v in c.items(): print("{} count: {}".format(k, v)
Output:
drugs count: 2 shoplifting count: 2 other-theft count: 6 anti-social-behaviour count: 79 violent-crime count: 9 criminal-damage-arson count: 12 vehicle-crime count: 7 public-disorder-weapons count: 3 other-crime count: 11 burglary count: 4