Python tuple program to filter out tuples that have more than 3 elements

In this python tuple program, we will filter out tuples that have more than 3 elements.

Steps to solve the program
  1. Take a list of tuples as input and create an empty list.
  2. Use for loop to iterate over tuples in the list.
  3. 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.
  4. 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)]
				
			

calculate the frequency of elements in a tuple.

assign the frequency of tuples to each tuple.

Leave a Comment