top of page

Python program to create Homogeneous Tuple from Heterogeneous Tuple

Kritika Rajput

In this program, we will make tuples having the same data type value from a tuple having different data type elements as we know that tuple is immutable so for this we will create a list than to iterate the tuple elements we will use for loop so after iterating the element. we will put all the same elements in one list after insert all same element in one list then we will convert the lost into a tuple and finally, we will print all the tuple we have converted.


Impelementation:

x=(10,5,6.0,15,5+4j,'programmersdoor',7.2)
t1,t2,t3,t4,t5=[],[],[],[],[]
for e in x:
    if type(e) == int:
        t1.append(e)
    elif type(e) == float:
        t2.append(e)
    elif type(e) == complex:
        t3.append(e)
    elif type(e) == str:
        t4.append(e)
t1=tuple(t1)
t2=tuple(t2)
t3=tuple(t3)
t4=tuple(t4)
print(t1,t2,t3,t4,sep='\n')
Output:
(10, 5, 15)
(6.0, 7.2)
((5+4j),)
('programmersdoor',)
 

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor


Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

18 views0 comments

Recent Posts

See All

Comments


bottom of page