Ben Goold
Ben Goold

Reputation: 1

Counting occurences of string without knowing the string

I have a list of approximately 20000 strings and I want to count how often each string occurs in the list, however I don't know all the different unique strings in the list. I have tried experimenting with the list.count('string') but without knowing all the unique strings. Is there anyway of reading the list and getting an output of all the unique strings present. Thanks for any help

Upvotes: 0

Views: 24

Answers (1)

Fabian
Fabian

Reputation: 1150

I think this will solve your problem.

from collections import Counter

words = ["hello", "world", "hello", "world"]

print(Counter(words)) #Counter({'hello': 2, 'world': 2})

For Reference check Counter

Upvotes: 1

Related Questions