Count letter frequency in word list, excluding duplicates in the same word

A variation on @Primusa answer without using update:

from collections import Counter

words = ["tree", "bone", "indigo", "developer"]
counts = Counter(c for word in words for c in set(word.lower()) if c.isalpha())

Output

Counter({'e': 3, 'o': 3, 'r': 2, 'd': 2, 'n': 2, 'p': 1, 'i': 1, 'b': 1, 'v': 1, 'g': 1, 'l': 1, 't': 1})

Basically convert each word to a set and then iterate over each set.

Leave a Comment