Python Practice › Tuple Programs › Program 59

Remove nested tuples

EasyTuple Programs · 59 of 70

Write a Python program to remove nested tuples from a tuple.

Advertisement
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

  1. Keep items that are not tuples.

Solution

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

More tuple programs

← All 70 Tuple Programs

Leave a Comment