Python Practice › Tuple Programs › Program 59
Remove nested tuples
Write a Python program to remove nested tuples from a tuple.
Input
("s", "q", "a", ("t", "o", "o", "l", "s"), "i", "s", "b", "e", "s", "t")Output
('s', 'q', 'a', 'i', 's', 'b', 'e', 's', 't')Approach
- Keep items that are not tuples.
Solution
t = ("s", "q", "a", ("t", "o", "o", "l", "s"), "i", "s", "b", "e", "s", "t")print(tuple(x for x in t if not isinstance(x, tuple)))Output
('s', 'q', 'a', 'i', 's', 'b', 'e', 's', 't')Learn the concept
New to this topic? Read the lesson, then come back.