-2

I know I can do something like below to get number of occurrences of elements in the list:

from collections import Counter

words = ['a', 'b', 'c', 'a']

Counter(words).keys() # equals to list(set(words))
Counter(words).values() # counts the elements' frequency

Outputs:

['a', 'c', 'b']
[2, 1, 1]

But I want to get the count 2 for b and c as b and c occur exactly once in the list. Is there any way to do this in concise / pythonic way without using Counter or even using above output from Counter?

Rnj
  • 1,067
  • 1
  • 8
  • 23

1 Answers1

-1

You could just make an algorithm that does that, here is a one liner (thanks @d.b):

sum(x for x in Counter(words).values() if x == 1)

Or more than one line:

seen = []
count = 0

for word in words:
  if word not in seen:
    count += 1
    seen.append(word)
kenan238
  • 398
  • 4
  • 13