0%

🪄 Measure The Hush

Count what remains when noise is gone. A set removes duplicates, and len() measures the silence.
🔮 print(len({c for c in “COFFEE”}))

A word echoes with repetition. C O F F E E. The same letters return. The F calls twice. The E answers twice. But what happens when you strip away the echoes? What remains after the noise is gone? The set comprehension {c for c in “COFFEE”} keeps only unique characters. Then len() counts them. The result is the number of distinct letters. The hush after the storm of repetition.

🕯️ Magic Note

A set in Python cannot contain duplicate values. When you iterate through a string and add each character to a set, each character is stored only once regardless of how many times it appears. The set comprehension creates this collection in one clean line. Then len() reveals how many unique characters survived the filtering.

The syntax {c for c in “COFFEE”} creates a set containing the letters C, O, F, E. Each appears only once even though F and E repeat. The len() function counts the elements in this set, returning 4. The noise fades. The silence is measured.
  • Set comprehensions use curly braces like dictionary comprehensions but without key-value pairs
  • The result is a set containing unique elements from the iterable
  • Order is not preserved because sets are unordered
  • Equivalent to len(set(“COFFEE”)) which is simpler for this specific use case
💡 For counting unique characters in a string, len(set(string)) is simpler and more readable than a set comprehension. Set comprehensions shine when you need to apply a transformation or filter condition: {c.lower() for c in text if c.isalpha()}. Use the simpler form when possible, the comprehension when you need control.
StringSet of Unique CharactersCount
“COFFEE”{“C”, “O”, “F”, “E”}4
“BANANA”{“B”, “A”, “N”}3
“MISSISSIPPI”{“M”, “I”, “S”, “P”}4
“PYTHON”{“P”, “Y”, “T”, “H”, “O”, “N”}6
“aaa”{“a”}1
⚠️ Set comprehensions create unordered collections. The order of elements in the resulting set may not match the original string order. Also, set comprehensions create a new set each time they run. For very large strings, this is memory efficient because each character is stored only once, but the iteration still processes every character.
Examples

Python

# Count unique characters in a word

word = “COFFEE”

unique_count = len({c for c in word})

print(unique_count)

# Output: 4

Python

# Simpler way: using set() directly

word = “COFFEE”

unique_count = len(set(word))

print(unique_count)

# Output: 4

# set(word) does the same as the comprehension

Python

# Filtering before counting: only letters

text = “C0FF33!”

letters_only = {c for c in text if c.isalpha()}

print(letters_only)

# Output: {“C”, “F”}

print(len(letters_only))

# Output: 2

Common Mistakes
  • Confusing set comprehension {c for c in word} with dictionary comprehension which requires key-value pairs
  • Forgetting that sets are unordered, expecting the count to preserve the original order of characters
  • Using set comprehension when set(word) would be simpler and more readable for basic unique character extraction

⚡ Whisper

The coffee cup holds many sips, but only a few flavors. The word echoes with repetition, but the silence after reveals the truth. Count what remains when the noise is gone. That is the measure of essence.