In this python tuple program, we will filter out tuples that have more than 3 elements.
Steps to solve the program
- Take a list of tuples as input and create an empty list.
- Use for loop to iterate over tuples in the list.
- If the length of the list is more than 3 i.e. it has more than 3 elements add such tuples to the empty list.
- Print the output.
l = [(1,4),(4,5,6),(2,),(7,6,8,9),(3,5,6,0,1)]
l1 = []
print("Original list of tuples: ",l)
for tup in l:
if len(tup)>3:
l1.append(tup)
print("After removing tuples: ",l1)
Output :
Original list of tuples: [(1, 4), (4, 5, 6), (2,), (7, 6, 8, 9), (3, 5, 6, 0, 1)]
After removing tuples: [(7, 6, 8, 9), (3, 5, 6, 0, 1)]