Python Practice › Tuple Programs › Program 51

Unique digits from tuples

EasyTuple Programs · 51 of 70

Write a Python program to extract the unique digits of all numbers in a list of tuples.

Advertisement
Input
[(6, 87, 7), (4, 53), (11, 28, 3)]
Output
[1, 2, 3, 4, 5, 6, 7, 8]

Approach

  1. Join every number into one string and collect its distinct digits.

Solution

Python
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.

More tuple programs

← All 70 Tuple Programs

1 thought on “Unique digits from tuples”

Leave a Comment