Skip to content

Instantly share code, notes, and snippets.

@kayalshri
Created March 15, 2019 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayalshri/f484b1e205437adfe6af4ee314cdf134 to your computer and use it in GitHub Desktop.
Save kayalshri/f484b1e205437adfe6af4ee314cdf134 to your computer and use it in GitHub Desktop.
Python :: Count Unique Elements
## Problem : Get Number of unique elements in a list
x = ['f', 'e', 'e', 'f', 'f']
# output {'e': 2, 'f': 3}
##Solution
o = {i:x.count(i) for i in x}
print(o)
## Problem : Get Number of unique values in a dict
y = {'a': 1, 'c': 5, 'b': 4, 'd': 6, 'e':5, 'f':4, 'g':1, 'h':5}
# output {1: 2, 4: 2, 5: 3, 6: 1}
## Solution
o = {i:y.values().count(i) for i in y.values()}
print(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment