Find the last element of a tuple using negative indexing

In this python tuple program, we will find the last element of a tuple using negative indexing.

Steps to solve the program
  1. Take a tuple as input.
  2. Find the last element of a tuple using tuple_name[-1].
  3. Print the output.
				
					tup = ('p','y','t','h','o','n')
print("Original tuple: ",tup)
print("Last element of the tuple using negative indexing: ",tup[-1])
				
			

Output :

				
					Original tuple:  ('p', 'y', 't', 'h', 'o', 'n')
Last element of the tuple using negative indexing:  n
				
			

check the type of the input and return True if the type is a tuple and False if it is not a tuple.

Check the type of the input and return True if the type is a tuple

In this python tuple program, we will check the type of the input and return True if the type is a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. Use an if-else statement and print True if the type is a tuple and False if it is not a tuple.
  3. Print the output.
				
					tup = (7,4,9,2,0)
if type(tup) == tuple:
    print("True")
else:
    print("False")
				
			

Output :

				
					True
				
			

program to swap tuples.

find the last element of a tuple using negative indexing.

Python tuple program to swap the tuples

In this python tuple program, we will swap the tuples.

Steps to solve the program
  1. Take two tuples as input.
  2. Swap the tuples by using the  ” operator i.e. a,b=b,a.
  3. Print the output.
				
					A=(7,4,9)
B=(3,)
print(f"tuple1: {A}, tuple2: {B}")
A,B = B,A
print(f"tuple1: {A}, tuple2: {B}")
				
			

Output :

				
					tuple1: (7, 4, 9), tuple2: (3,)
tuple1: (3,), tuple2: (7, 4, 9)
				
			

calculate the average of the elements in the tuple.

check the type of the input and return True if the type is a tuple and False if it is not a tuple.

Program to calculate the average of the elements in the tuple

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

Steps to solve the program
  1. Take a tuple as input and create a variable add and set its value equal to 0.
  2. Use a for loop to iterate over numbers in the tuple.
  3. Add the numbers to the add variable.
  4. Find the average of the elements in the tuple by dividing the value of add variable by the length of the tuple i.e number of elements in the tuple.
  5. Print the output.
				
					tup = (5,3,9,6)
print("Original tuple: ",tup)
add = 0
for ele in tup:
    add += ele
print("Average of elements in the tuple: ",add/len(tup))
				
			

Output :

				
					Original tuple:  (5, 3, 9, 6)
Average of elements in the tuple:  5.75
				
			

count the total number of unique tuples.

program to swap tuples.

Python tuple program to convert a binary tuple to an integer

In this python tuple program, we will convert a binary tuple to an integer.

Steps to solve the program
				
					tup = (1,0,0)
print("Original tuple is : ",tup) 
result = int("".join(str(val) for val in tup), 2)
print("Decimal number: ",result)
				
			

Output :

				
					Original tuple is :  (1, 0, 0)
Decimal number:  4
				
			

find common elements between two lists of tuples.

count the total number of unique tuples.

Python tuple program to count the total number of unique tuples in a list

In this python tuple program, we will count the total number of unique tuples in a list.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Create a set of the given list to remove duplicate tuples using set().
  3. Now count the total number of unique tuples using len().
  4. Print the output.
				
					l =[(8,9),(4,7),(3,6),(8,9)]
print("Original list of tuples: ",l)
print("Total number of unique tuples: ",len(set(l)))
				
			

Output :

				
					Original list of tuples:  [(8, 9), (4, 7), (3, 6), (8, 9)]
Total number of unique tuples:  3
				
			

convert a binary tuple to an integer.

calculate the average of the elements in the tuple.

Find common elements between two lists of tuples

In this python tuple program, we will find common elements between two lists of tuples.

Steps to solve the program
  1. Take two lists of tuples as input.
  2. Create a set of two lists to remove duplicate tuples by using set().
  3. Find the common tuples between two lists of tuples using the ” operator.
  4. Print the output.
				
					tup1 = [(1,5),(4,8),(3,9)]
tup2 = [(3,9),(5,6),(1,5),(0,4)]
print(f"Tuples: {tup1} and {tup2}")
result = (set(tup1) & set(tup2))
print("Common elements between tuples: ",result)
				
			

Output :

				
					Tuples: [(1, 5), (4, 8), (3, 9)] and [(3, 9), (5, 6), (1, 5), (0, 4)]
Common elements between tuples:  {(3, 9), (1, 5)}
				
			

order tuples by external list.

convert a binary tuple to an integer.

Python tuple program to order tuples by external list

In this python tuple program, we will order tuples by external list.

Steps to solve the program
  1. Take a list of tuples and a list by which we have to order tuples as input.
  2. Create a dictionary of a list of tuples.
  3. It will set words in the tuples as keys and numbers as their values.
  4. Use a for loop to iterate over words in the list by which we have to order the tuples.
  5. Now add the word and its value in the dictionary to another list using list comprehension i.e. loop inside the list.
  6. Print the output.
				
					list1 = [('very',8),('i',6),('am',5),('happy',0)]
list2 = ['i','am','very','happy']
print("List of tuple: ",list1)
print("List: ",list2)
d = dict(list1)
result = [(key, d[key]) for key in list2]
print("Output list: ",result)
				
			

Output :

				
					List of tuple:  [('very', 8), ('i', 6), ('am', 5), ('happy', 0)]
List:  ['i', 'am', 'very', 'happy']
Output list:  [('i', 6), ('am', 5), ('very', 8), ('happy', 0)]
				
			

concatenate two tuples.

find common elements between two lists of tuples.

Python tuple program to concatenate two tuples

In this python tuple program, we will concatenate two tuples.

Steps to solve the program
  1. Take two tuples as input.
  2. Concatenate the two tuples using the ” operator.
  3. Print the output.
				
					tup1 = ('s','q','a')
tup2 = ('t','o','o','l')
print(f"Original tuple: {tup1} {tup2}")
result = tup1+tup2
print("Combined tuple: ",result)
				
			

Output :

				
					Original tuple: ('s', 'q', 'a') ('t', 'o', 'o', 'l')
Combined tuple:  ('s', 'q', 'a', 't', 'o', 'o', 'l')
				
			

sort a list of tuples by the minimum value of a tuple.

order tuples by external list.

Python tuple program to sort a list if tuples by the minimum value of a tuple

In this python tuple program, we will sort a list if tuples by the minimum value of a 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 minimum value to the key inside the sorted().
  3. Print the output.
				
					l = [(1,5,7),(3,4,2),(4,9,0)]
print("Original list of tuples: ",l)
l1 = sorted(l,key = lambda x: min(x))
print("After sorting: ",l1)
				
			

Output :

				
					Original list of tuples:  [(1, 5, 7), (3, 4, 2), (4, 9, 0)]
After sorting:  [(4, 9, 0), (1, 5, 7), (3, 4, 2)]
				
			

sort a tuple by the maximum value of a tuple.

concatenate two tuples.