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.

Python tuple program to convert a tuple into a dictionary

In this python tuple program, we will convert a tuple into a dictionary.

Steps to solve the program
  1. Take tuples inside a tuple as input.
  2. Convert the tuple into a dictionary using dict().
  3. Print the output.
				
					tup = ((5,'s'),(6,'l'))
print("Tuple: ",tup)
D = dict(tup)
print("Dictionary: ",D)
				
			

Output :

				
					Tuple:  ((5, 's'), (6, 'l'))
Dictionary:  {5: 's', 6: 'l'}
				
			

find the length of a tuple.

program to reverse a tuple.

Python tuple program to find the length of a tuple

In this python tuple program, we will find the length of a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. Find the length of a tuple using len().
  3. Print the output.
				
					tup = ('v','i','r','a','t')
print("Original tuple: ",tup)
print("Length of the tuple: ",len(tup))
				
			

Output :

				
					Original tuple:  ('v', 'i', 'r', 'a', 't')
Length of the tuple:  5
				
			

find an index of an element in a tuple.

convert a tuple into a dictionary.