Menu

gitpiper

frequencies python Code Snippet in 2024

listintermediate

Last Updated: 20 April 2024

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 }

python snippet similar to frequencies For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️