Python Practice › Tuple Programs › Program 51
Unique digits from tuples
Write a Python program to extract the unique digits of all numbers in a list of tuples.
Input
[(6, 87, 7), (4, 53), (11, 28, 3)]
Output
[1, 2, 3, 4, 5, 6, 7, 8]
Approach
- Join every number into one string and collect its distinct digits.
Solution
data = [(6, 87, 7), (4, 53), (11, 28, 3)]digits = {int(ch) for t in data for n in t for ch in str(n)}print(sorted(digits))Output
[1, 2, 3, 4, 5, 6, 7, 8]
Line by line
Line 2
The nested comprehension visits every number, then every digit of it.
Learn the concept
New to this topic? Read the lesson, then come back.
1 thought on “Unique digits from tuples”