from collections import defaultdict from math import floor def count_by(lists, fn = lambda x: x): count = defaultdict(int) for val in map(fn, lists): count[val] += 1 return dict(count) count_by([6.1, 4.2, 6.3], floor) # {6: 2, 4: 1}
count_by(['one', 'two', 'three'], len) # {3: 2, 5: 1}