Program to count the elements in a list if an element is a tuple

In this python tuple program, we will count the elements in a list if an element is a tuple.

Steps to solve the program
  1. Take a list as input.
  2. Use a for loop to iterate over each element in the list.
  3. If the type of an element is a tuple then count the number of elements in it and print the output.
  4. Use type() and len() for this purpose.
  5. Print the output.
				
					l = [1,6,8,5,(4,6,8,0),5,4]
print("Original list: ",l)
for val in l:
    if type(val) is tuple:
        print(f"Elements in the tuple {val}: ",len(val))
				
			

Output :

				
					Original list:  [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]
Elements in the tuple (4, 6, 8, 0):  4
				
			

sort a tuple by its float element.

multiply all the elements in a tuple.

Python tuple program to sort a tuple by its float element

In this python tuple program, we will sort a tuple by its float element.

Steps to solve the program
  1. Take a list of tuples having float elements in it as input.
  2. We can see that inside each tuple float element is at index 1 in each tuple.
  3. Sort the tuple by its float element using sorted().
  4. Inside sorted pass the lambda function to the key to sort the tuple by float element i.e. sorted(tup,key = lambda x: float(x[1])).
  5. Print the output.
				
					l = [(3,5.6),(6,2.3),(1,1.8)]
print("Original list of tuples: ",l)
l1 = sorted(tup,key = lambda x: float(x[1]))
print("After sorting: ",l1)
				
			

Output :

				
					Original list of tuples:  [(3, 5.6), (6, 2.3), (1, 1.8)]
After sorting:  [(1, 1.8), (6, 2.3), (3, 5.6)]
				
			

remove empty tuples from a list of tuples.

count the elements in a list if an element is a tuple.

Python tuple program to remove empty tuples from a list of tuples

In this python tuple program, we will remove empty tuples from a list of tuples.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use for loop to iterate over each tuple.
  3. If the length of the tuple is equal to 0 i.e. tuple is empty then remove that tuple from the list using remove().
  4. Print the output.
				
					l = [(" ",),('a','b'),('a','b','c'),('d',),()]
print("Original list of tuples: ",l)
for tup in l:
    if len(tup) == 0:
        l.remove(tup)
print("After removing empty tuples: ",l)
        
				
			

Output :

				
					Original list of tuples:  [(' ',), ('a', 'b'), ('a', 'b', 'c'), ('d',), ()]
After removing empty tuples:  [(' ',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
				
			

convert a tuple to string datatype.

sort a tuple by its float element.

Python tuple program to convert a tuple to string datatype

In this python tuple program, we will convert a tuple to string datatype.

Steps to solve the program
  1. Take a tuple input.
  2. Convert the tuple to a string datatype using str().
  3. Print the output.
				
					tup = (4,1,7,5)
print("Original tuple: ",tup)
tup1 = str(tup)
print("After converting to a string: ",tup1)
print("Type of new tuple: ",type(tup1))
				
			

Output :

				
					Original tuple:  (4, 1, 7, 5)
After converting to a string:  (4, 1, 7, 5)
Type of new tuple:  <class 'str'>
				
			

test whether a tuple is distinct or not.

remove empty tuples from a list of tuples.

Python tuple program to test whether a tuple is distinct or not

In this python tuple program, we will test whether a tuple is distinct or not.

Steps to solve the program
  1. Take a tuple as input.
  2. Create a set of input tuple it will remove the duplicate elements.
  3. Now check whether the length of the tuple is equal to the length of the new tuple i.e. length of set of tuple using an if-else statement.
  4. If they are equal then the tuple is distinct else the tuple is not distinct.
  5. Print the output.
				
					tup = (1,2,3,4)
if len(set(tup)) == len(tup):
    print("Tuple is distinct")
else:
    print("Tuple is not distinct")
				
			

Output :

				
					Tuple is distinct
				
			
				
					tup1 = (3,4,5,4,6,3)
if len(set(tup1)) == len(tup1):
    print("Tuple is distinct")
else:
    print("Tuple is not distinct")
				
			

Output :

				
					Tuple is not distinct
				
			

find values of tuples at ith index number.

convert a tuple to string datatype.

Python program to find values of tuples at ith index number

In this python tuple program, we will find values of tuples at ith index number.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Find the values of the tuple at the 3rd index in the list of tuples using indexing.
  3. Print the output.
				
					l = [(1,2,3),(6,5,4),(7,6,8),(9,0,1)]
print("Tuple at index 3: ",l[3])
				
			

Output :

				
					Tuple at index 3:  (9, 0, 1)
				
			

assign the frequency of tuples to each tuple.

test whether a tuple is distinct or not.

Program to assign the frequency of tuples to each tuple

In this python tuple program, we will assign the frequency of tuples to each tuple.

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 from the list.
  3. If a tuple is not in the dictionary then add that tuple as a key and 0 as its value.
  4. If the tuple is already in the list then add 1 to the value.
  5. Print the output.
				
					l = [('s','q'),('t','o','o','l'),('p','y'),('s','q')]
print("Original list of tuples: ",l)
d = {}
for tup in l:
    if tup not in d:
        d[tup] = 0
    if tup in d:
        d[tup] += 1
print("Frequency count : ",d)
				
			

Output :

				
					Original list of tuples:  [('s', 'q'), ('t', 'o', 'o', 'l'), ('p', 'y'), ('s', 'q')]
Frequency count :  {('s', 'q'): 2, ('t', 'o', 'o', 'l'): 1, ('p', 'y'): 1}
				
			

filter out tuples that have more than 3 elements.

find values of tuples at ith index number.

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.