Creates a dictionary with the unique values of a list as keys and their frequencies as the values.
collections.defaultdict
to store the frequencies of each unique element.dict()
to return a dictionary with the unique elements of the list as keys and their frequencies as the values.from collections import defaultdict
def frequencies(lst):
freq = defaultdict(int)
for val in lst:
freq[val] += 1
return dict(freq)
frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']) # { 'a': 4, 'b': 2, 'c': 1 }
Python, List
Creates a list with the non-unique values filtered out.
Python, List
Creates a list with the unique values filtered out.
Python, List
Converts a list of dictionaries into a list of values corresponding to the specified key
.