Files
30-seconds-of-code/snippets/frequencies.md
Isabelle Viktoria Maciohsek 18a8c6cfbc Update snippet titles
2022-02-13 14:06:41 +02:00

677 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
Value frequencies list,intermediate 2020-03-15T12:54:08+02:00 2020-11-02T19:27:53+02:00

Creates a dictionary with the unique values of a list as keys and their frequencies as the values.

  • Use collections.defaultdict to store the frequencies of each unique element.
  • Use 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 }