0%

🪄 If It Repeats, It Speaks

Finds the first repeating character in a string. If nothing repeats, returns None instead of crashing.
🔮 dupe = min((c for c in s if s.count(c)>1), default=None)

A string whispers its characters. Sometimes one of them echoes twice. You want to catch that echo. The first character that appears more than once. But what happens when the string has no repeats? What happens when every character is silent and unique?

🕯️ Magic Note

The min() function normally raises a ValueError when called on an empty iterable. The default parameter changes this behavior. When the iterable is empty, min() returns the default value instead of throwing an error. This turns a crash into a graceful silence.

The syntax min((c for c in s if s.count(c)>1), default=None) first creates a generator of characters that repeat. Then min() finds the smallest (alphabetically first) among them. If no characters repeat, the generator is empty. The default=None ensures the result is None instead of a ValueError.
  • The default parameter works in both min() and max() functions
  • Available from Python 3.4 onwards
  • Prevents ValueError: min() arg is an empty sequence
  • Can use any default value, not just None
💡 Always consider using default when calling min() or max() on potentially empty iterables.
This is especially useful when filtering data that might return no results. Common defaults include 0, “”, False, or a custom sentinel value like “no match”.
Input StringGenerator Resultmin() with default
“magic”No repeating charsNone
“python”No repeating charsNone
“hello”“h”, “l”“h”
“mississippi”“m”, “i”, “s”, “p”“i”
“aa”“a”“a”
⚠️ The s.count(c) inside the generator makes this solution inefficient for long strings. It counts the entire string for each character. For large strings, consider using collections.Counter instead. Also remember that min() returns the smallest character alphabetically, not necessarily the first repeating character.
Examples

Python

# Safe min() with default

s = “unique”

dupe = min((c for c in s if s.count(c)>1), default=None)

print(dupe)

# Output: None

# No ValueError, just silence

Python

# Without default (dangerous)

s = “unique”

try:

dupe = min(c for c in s if s.count(c)>1)

except ValueError as e:

print(f”Spell failed: {e}”)

# Output: Spell failed: min() arg is an empty sequence

Python

# Using a custom default value

s = “no repeats here”

dupe = min((c for c in s if s.count(c)>1), default=”silence”)

print(dupe)

# Output: “silence” (depending on actual repeats in the string)

Common Mistakes
  • Forgetting the default parameter exists and handling empty sequences with try/except instead
  • Using default in Python versions older than 3.4, causing a TypeError
  • Assuming min() with default is always the best choice, sometimes an explicit check is more readable

⚡ Whisper

A spell that crashes in silence is no spell at all. The default is your safety net. It catches nothing and returns peace. Even when there is no echo, the magic still completes.