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.

Calculate the frequency of elements in a tuple

In this python tuple program, we will calculate the frequency of elements in a tuple.

Steps to solve the program
  1. Take a tuple as input and create an empty dictionary.
  2. Use for loop to iterate over each character from the tuple.
  3. If a character is not in the dictionary then add that character as a key and 0 as its value.
  4. If the character is already in the list then add 1 to the value.
  5. Print the output.
				
					tup = ('a','b','c','d','b','a','b')
print("Original tuple: ",tup)
d = {}
for char in tup:
    if char not in d:
        d[char] = 0
    if char in d:
        d[char] += 1
print("Frequency count : ",d)
				
			

Output :

				
					Original tuple:  ('a', 'b', 'c', 'd', 'b', 'a', 'b')
Frequency count :  {'a': 2, 'b': 3, 'c': 1, 'd': 1}
				
			

sort a list of tuples by the length of the tuple.

filter out tuples that have more than 3 elements.

Python tuple program to sort a list of tuples by the length of the tuple

In this python tuple program, we will sort a list of tuples by the length of the tuple.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use sorted() to sort the tuple and pass the lambda function to sort the tuple by the length of the tuple i.e. len() function to the key inside the sorted().
  3. Print the output.
				
					l =  [(4, 5, 6), ( 6, ), ( 2, 3), (6, 7, 8, 9, 0 ) ]
print("Original list of tuples: ",l)
res = sorted(l, key=lambda tup: len(tup))
print("After sorting list of tuples by length of tuples: ",res)
				
			

Output :

				
					Original list of tuples:  [(4, 5, 6), (6,), (2, 3), (6, 7, 8, 9, 0)]
After sorting list of tuples by length of tuples:  [(6,), (2, 3), (4, 5, 6), (6, 7, 8, 9, 0)]
				
			

sort a list of tuples by the second item.

calculate the frequency of elements in a tuple.

Python tuple program to sort a list of tuples by the second item

In this python tuple program, we will sort a list of tuples by the second item.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use sorted() to sort the tuple and pass the lambda function to sort the tuple by the second item to the key inside the sorted().
  3. Print the output.
				
					l = [(1,5),(7,8),(4,0),(3,6)]
print("Original list of tuples: ",l)
res = sorted(l, key=lambda tup: tup[1])
print("After sorting list of tuples by second element: ",res)
				
			

Output :

				
					Original list of tuples:  [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by second element:  [(4, 0), (1, 5), (3, 6), (7, 8)]
				
			

sort a list of tuples by the first item.

sort a list of tuples by the length of the tuple.

Python tuple program to sort a list of tuples by the first item

In this python tuple program, we will sort a list of tuples by the first item.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use sorted() to sort the tuple and pass the lambda function to sort the tuple by the first item to the key inside the sorted().
  3. Print the output.
				
					l = [(1,5),(7,8),(4,0),(3,6)]
print("Original list of tuples: ",l)
res = sorted(l, key=lambda tup: tup[0])
print("After sorting list of tuples by first element: ",res)
				
			

Output :

				
					Original list of tuples:  [(1, 5), (7, 8), (4, 0), (3, 6)]
After sorting list of tuples by first element:  [(1, 5), (3, 6), (4, 0), (7, 8)]
				
			

remove Tuples from the List having every element as None.

sort a list of tuples by the second item.

Remove Tuples from the List having every element as None

In this python tuple program, we will remove Tuples from the List having every element as None.

Steps to solve the program
  1. Take a list of tuples as input and create an empty list.
  2. Use a for loop to iterate over tuples in the list.
  3. Use the following condition to check whether all the elements in the tuple are equal to None.
  4. if not all(ele == None for ele in tup.
  5. If not then add those tuples in the empty list using append().
  6. Print the output.
				
					l = [(None,),(None,None),(5,4),(1,6,7),(None,1)]
print("Origianl list: ",l)
result = []
for tup in l:
    if not all(ele == None for ele in tup):
        result.append(tup)        
print("After removing tuples: ",result)
				
			

Output :

				
					Origianl list:  [(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]
After removing tuples:  [(5, 4), (1, 6, 7), (None, 1)]
				
			

remove tuples from the List having an element as None.

sort a list of tuples by the first item.

Remove tuples from the List having an element as None

In this python tuple program, we will remove tuples from the List having an element as None.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use for loop to iterate over tuples in the list.
  3. Use another nested loop to iterate over elements in the tuples.
  4. In an element in the tuple is equal to None then remove that tuple from the list.
  5. Print the output.
				
					l =  [(None,),(5, 4),(1,6,7),(None,1)]
print("Origianl list: ",l)
for tup in l:
    for ele in tup:
        if ele == None:
            l.remove(tup)

print("After removing tuples: ",l)
				
			

Output :

				
					Origianl list:  [(None,), (5, 4), (1, 6, 7), (None, 1)]
After removing tuples:  [(5, 4), (1, 6, 7)]
				
			

remove tuples of length i.

remove Tuples from the List having every element as None.

Python tuple program to remove tuples of length i

In this python tuple program, we wil remove tuples of length i.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use for loop to iterate over each tuple in the list.
  3. If the length of the tuple is equal to 2 then remove that tuple from the list using remove().
  4. Print the output.
				
					list1 = [(2,5,7),(3,4),(8,9,0,5)]
print("Original list: ",list1)
for tup in list1:
    if len(tup) == 2:
        list1.remove(tup)
        
print("After removing tuple having length 2: ",list1)
				
			

Output :

				
					Original list:  [(2, 5, 7), (3, 4), (8, 9, 0, 5)]
After removing tuple having length 2:  [(2, 5, 7), (8, 9, 0, 5)]
				
			

remove tuples from the List having an element as None.

Program to convert a list of tuples in a dictionary

In this python tuple program, we will convert a list of tuples in a dictionary.

Steps to solve the program
  1. Take a list of tuples as input and create an empty dictionary.
  2. Use for loop to iterate over each tuple in the list.
  3. Use D.setdefault(a, []).append(b) it will set the first value in the tuple as the key and create an empty list to store its values.
  4. a is the key and b is the valueis the name of the dictionary.
  5. If there is a tuple in the list whose key has been previously used then it will not create a new key-value pair instead it will add the value of the tuple to the existing list of key-value pair.
  6. Print the output.
				
					l =  [('s',2),('q',1),('a',1),('s',3),('q',2),('a',4)]
print("List of tuples: ",l)
D = {}
for a, b in l:
    D.setdefault(a, []).append(b)

print("Dictionary: ",D)
				
			

Output :

				
					List of tuples:  [('s', 2), ('q', 1), ('a', 1), ('s', 3), ('q', 2), ('a', 4)]
Dictionary:  {'s': [2, 3], 'q': [1, 2], 'a': [1, 4]}
				
			

program to reverse a tuple.

Python tuple program to reverse a tuple

In this python tuple program, we will reverse a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. FIrst reverse the given tuple by using reversed() and then convert it to a tuple using tuple().
  3. Print the output.
				
					tup = (4,6,8,3,1)
print("Original tuple: ",tup)
tup1 = tuple(reversed(tup))
print("Reversed tuple: ",tup1)
				
			

Output :

				
					Original tuple:  (4, 6, 8, 3, 1)
Reversed tuple:  (1, 3, 8, 6, 4)
				
			

convert a tuple into a dictionary.

convert a list of tuples in a dictionary.