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.

Python tuple program to slice a tuple

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

Steps to solve the program
  1. Create a tuple.
  2. Slice the given tuple using tuple_name[start_index:end_index].
  3. Print the output.
				
					tup = (5,7,3,4,9,0,2)
print("Original tuple: ",tup)
print(tup[:3])
print(tup[2:5])
				
			

Output :

				
					Original tuple:  (5, 7, 3, 4, 9, 0, 2)
(5, 7, 3)
(3, 4, 9)
				
			

remove an item from a tuple.

find an index of an element in a tuple.

Python tuple program to remove an item from a tuple

In this python tuple program, we will remove an item from a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Now convert that tuple to a list using list().
  3. Remove the element from the list.
  4. Again convert the list to a tuple using tuple().
  5. Print the output.
				
					tup = ('p','y','t','h','o','n')
print("Original tuple: ",tup)
l = list(tup)
l.remove('h')
tup = tuple(l)
print("After removing an element: ",tup)
				
			

Output :

				
					Original tuple:  ('p', 'y', 't', 'h', 'o', 'n')
After removing an element:  ('p', 'y', 't', 'o', 'n')
				
			

convert a list into a tuple and multiply each element by 2.

program to slice a tuple.

Convert a list into a tuple and multiply each element

In this pythin tuple program, we will convert a list into a tuple and multiply each element.

Steps to solve the program
  1. Create a list and an empty list.
  2. Use for loop to iterate over elements in the list.
  3. Multiply each element by 2 and add it to the empty list.
  4. Now convert the new list to a tuple using tuple().
  5. Print the output.
				
					list1 = [12,65,34,77]
list2 = []
for ele in list1:
    a = 2*ele
    list2.append(a)
    
tup = tuple(list2)
print("Origianl list: ",list1)
print("After multiplyting by 2: ",tup)
				
			

Output :

				
					Origianl list:  [12, 65, 34, 77]
After multiplyting by 2:  (24, 130, 68, 154)
				
			

remove an item from a tuple.

Python tuple program to multiply adjacent elements of a tuple

In this python tuple program, we will multiply adjacent elements of a tuple.

Steps to solve the program
  1. Create a tuple and an empty list.
  2. Create a tuple having adjacent elements as a tuple inside of a tuple using zip(tup,tup[1:]).
  3. Use for loop to iterate over this tuple.
  4. Now get the product of the adjacent element and add it to the empty list.
  5. Convert the list back to the tuple using tuple().
  6. Print the output.
				
					tup =  (1,2,3,4)
list1 = []
for a,b in zip(tup,tup[1:]):
    c = a*b
    list1.append(c)
tup = tuple(list1)
print("Multiplying adjacent elements: ",tup)
				
			

Output :

				
					Multiplying adjacent elements:  (2, 6, 12)
				
			

create a tuple having squares of the elements from the list.

Create a tuple having squares of the elements from the list

In this python tuple program, we will create a tuple having squares of the elements from the list.

Steps to solve the program
  1. Create a tuple and convert it to a list using list().
  2. Use for loop to iterate over elements in the list.
  3. Find the index of the element using index().
  4. Get the square of the element.
  5. Now replace the square with the old element in the list.
  6. Now convert that list to a tuple using tuple().
  7. Print the output.
				
					tup = (1,3,5,7,6)
print("Origianl tuple: ",tup)
a = list()
for i in list(tup):
    b = i**2
    a.append(b)
tup = tuple(a)
print("After sqauring elements: ",tup)
				
			

Output :

				
					Origianl tuple:  (1, 3, 5, 7, 6)
After sqauring elements:  (1, 9, 25, 49, 36)
				
			

multiply adjacent elements of a tuple.

Python tuple program to add a list in the tuple

In this python tuple program, we will add a list in the tuple.

Steps to solve the program
  1. Create a list and a tuple.
  2. Convert the tuple into a list using list() and add another list to it using+ “.
  3. Now convert the combined list to a tuple using tuple().
  4. Print the output.
				
					list1 = [12,67]
tup = (6,8,4)
result = tuple(list(tup) + list1)
print(result)
				
			

Output :

				
					(6, 8, 4, 12, 67)
				
			

check whether an element exists in a tuple or not.

find sum of elements in a tuple.