How to Extract Monodigit Elements from a List in Python
A monodigit element is a number or string where every character is the same, like 888, "aaa", 4, or "ccc". Extracting such elements from a mixed list is useful in data validation, pattern detection, input filtering, and puzzle-solving algorithms.
In this guide, you will learn multiple methods to identify and extract monodigit elements from a Python list, from the most efficient one-liner to explicit loop-based approaches.
Understanding the Problem
Given a list containing numbers and strings:
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
Extract only elements where every character/digit is the same:
[888, "aaa", 111, 4, "ccc"]
888→ all digits are8✅"aaa"→ all characters area✅111→ all digits are1✅4→ single digit, trivially monodigit ✅"ccc"→ all characters arec✅463,123,"tut",939,112→ mixed characters ❌
Method 1: Using set() on String Conversion (Recommended)
The most elegant approach converts each element to a string, then checks if the set of its characters has length 1 - meaning all characters are identical:
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
result = [item for item in data if len(set(str(item))) == 1]
print(result)
Output:
[888, 'aaa', 111, 4, 'ccc']
How it works:
str(item)converts each element to its string representation (e.g.,888→"888").set(str(item))creates a set of unique characters (e.g.,{"8"}).- If the set has exactly one element, all characters are the same.
This is the cleanest and most Pythonic solution. The set() approach is immediately readable and works for both numbers and strings without any special handling.
Method 2: Using all() with List Comprehension
This approach explicitly checks if every character matches the first character:
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
result = [
item for item in data
if all(ch == str(item)[0] for ch in str(item))
]
print(result)
Output:
[888, 'aaa', 111, 4, 'ccc']
How it works:
str(item)[0]gets the first character of the element.all()checks that every characterchin the string matches the first one.- Only elements where all characters match are included in the result.
Method 3: Using String Multiplication
A clever technique compares the string against the first character repeated n times:
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
result = [
item for item in data
if str(item) == str(item)[0] * len(str(item))
]
print(result)
Output:
[888, 'aaa', 111, 4, 'ccc']
How it works:
str(item)[0] * len(str(item))creates a string of the first character repeated (e.g.,"8" * 3→"888").- If this matches the original string, all characters are identical.
Method 4: Using str.count()
This method counts occurrences of the first character and checks if it equals the total length:
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
result = []
for item in data:
s = str(item)
if s.count(s[0]) == len(s):
result.append(item)
print(result)
Output:
[888, 'aaa', 111, 4, 'ccc']
Method 5: Using filter() with lambda
A functional programming approach using filter():
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
result = list(filter(lambda item: len(set(str(item))) == 1, data))
print(result)
Output:
[888, 'aaa', 111, 4, 'ccc']
Creating a Reusable Function
For clean, reusable code:
def is_monodigit(item):
"""Check if all characters/digits in the item are the same."""
s = str(item)
return len(s) > 0 and len(set(s)) == 1
def extract_monodigit(data):
"""Extract all monodigit elements from a list."""
return [item for item in data if is_monodigit(item)]
# Test
data = [463, 888, 123, "aaa", 112, 111, "tut", 939, 4, "ccc"]
print(extract_monodigit(data))
# Works with various types
mixed = [0, 11, 222, "bb", "xyz", 7, "", 9999]
print(extract_monodigit(mixed))
Output:
[888, 'aaa', 111, 4, 'ccc']
[0, 11, 222, 'bb', 7, 9999]
Empty strings have len(set("")) equal to 0, not 1, so they are correctly excluded. Single-digit numbers and single-character strings are included because they trivially consist of one unique character.
Handling Edge Cases
Empty Strings and Zero
data = ["", 0, "a", 00, " "]
result = [item for item in data if str(item) and len(set(str(item))) == 1]
print(result)
Output:
[0, 'a', 0, ' ']
""→ empty string, excluded (no characters to evaluate)0→ string"0", one unique character ✅"a"→ one unique character ✅" "→ all spaces, one unique character ✅
Negative Numbers
data = [-111, -5, -22, 333]
result = [item for item in data if len(set(str(item))) == 1]
print(result)
Output:
[333]
Negative numbers include a - sign in their string representation, so -111 becomes "-111" with characters {"-", "1"}: two unique characters, not monodigit.
If you want to treat negative numbers by their digits only:
data = [-111, -5, -22, 333, -123]
result = [
item for item in data
if len(set(str(item).lstrip("-"))) == 1
]
print(result)
Output:
[-111, -5, -22, 333]
Common Mistake: Forgetting String Conversion
When working with mixed types, comparing digits directly without string conversion causes errors:
Wrong:
data = [888, "aaa", 111]
# This fails for strings: int("a") raises ValueError
result = [item for item in data if all(int(d) == int(str(item)[0]) for d in str(item))]
# ValueError: invalid literal for int() with base 10: 'a'
Correct - always use string comparison:
result = [item for item in data if len(set(str(item))) == 1]
Comparison of Methods
| Method | Readability | Conciseness | Performance | Best For |
|---|---|---|---|---|
set() length check | ✅ Highest | ✅ Highest | ✅ O(k) per element | General use (recommended) |
all() comparison | ✅ High | ⚠️ Moderate | ⚠️ O(k) with short-circuit | When early exit matters |
| String multiplication | ⚠️ Moderate | ✅ High | ✅ O(k) | Quick checks |
str.count() | ✅ High | ⚠️ Moderate | ✅ O(k) | Explicit counting |
filter() + lambda | ⚠️ Moderate | ✅ High | ✅ O(k) | Functional style |
Summary
Extracting monodigit elements from a list involves checking if all characters in an element's string representation are identical. Key takeaways:
- Use
len(set(str(item))) == 1for the cleanest, most Pythonic solution - it works for both numbers and strings in a single expression. - Use
all()when you want early termination on the first non-matching character. - Use string multiplication (
str(item)[0] * len(str(item))) for a clever alternative comparison. - Handle edge cases like empty strings (excluded), zeros (included), and negative numbers (decide whether to strip the minus sign).
- Wrap the logic in a reusable function for clean, maintainable code.