536 B
536 B
title, tags
| title | tags |
|---|---|
| frequencies | list,intermediate |
Returns a dictionary with the unique values of a list as keys and their frequencies as the values.
- Use
list.count()to get the frequency of each unique element. - Use
dict()constructor to return a dictionary with the unique elements of the list as keys and their frequencies as the values.
def frequencies(lst):
return dict((k, lst.count(k)) for k in lst)
frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']) # { 'a': 4, 'b': 2, 'c': 1 }