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.

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.