Write a Python program to Remove nested records from tuple


The program removes the nested tuples from a given tuple 'val'. It starts by initializing the given tuple val with multiple elements including nested tuples. Then, the program creates an empty list res to store the elements of the tuple after removing the nested tuples. Next, the program uses a for loop to iterate through each element of the given tuple val. For each element, the program checks if its data type is not a tuple by using the not type(i) is tuple statement. If the type of an element is not a tuple, the program appends it to the res list. Finally, the program converts the list res to a tuple and prints the result.

Source Code

val = (10, 20 , (30,), 40, (50, 60), 70)
print("Original Tuple : " , val)
 
res=[]
for i in val:
	if not type(i) is tuple:
		res.append(i)
res=tuple(res)
 
print("Removal of Nested Tuple : " ,res)

Output

Original Tuple :  (10, 20, (30,), 40, (50, 60), 70)
Removal of Nested Tuple :  (10, 20, 40, 70)

Example Programs