Python tuple program to remove duplicates from a tuple

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

Steps to solve the program
  1. Take a tuple as input.
  2. Use set() on the tuple to remove duplicates from it.
  3. After using the set() convert the outcome to a tuple using tuple().
  4. Print the output.
				
					tup = (6,4,9,0,2,6,1,3,4)
print("Original tuple: ",tup)
new_tup = tuple(set(tup))
print("New tuple: ",new_tup)
				
			

Output :

				
					Original tuple:  (6, 4, 9, 0, 2, 6, 1, 3, 4)
New tuple:  (0, 1, 2, 3, 4, 6, 9)
				
			

find the tuples with positive elements.

extract digits from a list of tuples.

Python tuple program to find the tuples with positive elements

In this python tuple program, we will find the tuples with positive elements.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use a for loop to iterate over tuples in the list.
  3. Use an if statement with any() function to find the tuples having positive elements in them.
  4. Print the output
				
					l = [(1,7),(-4,-5),(0,6),(-1,3)]
print("Original list of tuples: ",l)
for tup in l:
    if all(ele>=0 for ele in tup):
        print("Tuple having all positive element: ",tup)
				
			

Output :

				
					Original list of tuples:  [(1, 7), (-4, -5), (0, 6), (-1, 3)]
Tuple having all positive element:  (1, 7)
Tuple having all positive element:  (0, 6)
				
			

find tuples having negative elements.

remove duplicates from a tuple.

Python tuple program to find tuples having negative elements in it

In this python tuple program, we will find tuples having negative elements in it.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use a for loop to iterate over tuples in the list.
  3. Use any() function with an if statement to check whether elements in the tuples are negative.
  4. Print such tuples.
				
					l = [(1,7),(-4,-5),(0,6),(-1,3)]
for tup in l:
    if any(ele < 0 for ele in tup):
            print("Tuple having negative element: ",tup)
				
			

Output :

				
					Tuple having negative element:  (-4, -5)
Tuple having negative element:  (-1, 3)
				
			

find all the tuples that are divisible by a number.

find the tuples with positive elements.

Program to find all the tuples that are divisible by a number

In this python tuple program, we will find all the tuples that are divisible by a number.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use a for loop to iterate over tuples from the list.
  3. Create a count variable and assign its value equal to 0.
  4. Use a nested for loop to iterate over elements in the tuple.
  5. If an element is divisible by 5 then add 1 to the count variable.
  6. In the end, if the value of the count variable is equal to the length of the tuple i.e. total elements in the tuples, then that entire tuple is divisible by 5.
  7. Print the output.
				
					l = [(10,5,15),(25,6,35),(20,10)]
print("Original list of tuples: ",l)
for tup in l:
    count = 0
    for ele in tup:
        if ele%5 == 0:
            count += 1
    if count == len(tup):
        print("\nTuple in which all elements are divisible by 5: \n",tup)
				
			

Output :

				
					Original list of tuples:  [(10, 5, 15), (25, 6, 35), (20, 10)]

Tuple in which all elements are divisible by 5: 
 (10, 5, 15)

Tuple in which all elements are divisible by 5: 
 (20, 10)
				
			

convert a given list of tuples to a list of lists.

find tuples having negative elements.

Program to convert a list of tuples to a list of lists

In this python tuple program, we will convert a list of tuples to a list of lists.

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. Add the tuples to the empty list using append() after converting them to a list using list().
  4. Print the output.
				
					l = [(1,5),(7,8),(4,0)]
l1 = []
print("Original list: ",l)
for tup in l:
    l1.append(list(tup))
print("After converting tuples inside a list to list: ",l1)
				
			

Output :

				
					Original list:  [(1, 5), (7, 8), (4, 0)]
After converting tuples inside a list to list:  [[1, 5], [7, 8], [4, 0]]
				
			

compute the element-wise sum of tuples.

find all the tuples that are divisible by a number.

Python tuple program to compute the element-wise sum of tuples

In this python tuple program, we will compute the element-wise sum of tuples.

Steps to solve the program
  1. Take 3 tuples as input.
  2. Use the map() function and inside the map function use sum and combine the tuples using zip() to compute the element-wise sum of tuples.
  3. Convert the output from the map function to a tuple using tuple().
  4. Print the output.
				
					a = (1, 6, 7)
b = (4, 3, 0)
c = (2, 6, 8)
print(f"Original tuples: {a},{b},{c}")
s1 = map(sum, zip(a,b,c))
result = tuple(s1)
    
print("Element wise sum of the given tuples: ",result)
				
			

Output :

				
					Original tuples: (1, 6, 7),(4, 3, 0),(2, 6, 8)
Element wise sum of the given tuples:  (7, 15, 15)
				
			

convert a given tuple of integers into a number.

convert a given list of tuples to a list of lists.

convert a given tuple of integers into a number

In this python tuple program, we will convert a given tuple of integers into a number.

Steps to solve the program
  1. Take a tuple of integers as input and create an empty string.
  2. Use a for loop to iterate over integers from the tuple.
  3. Add integers to the string after converting them to string datatype using str(), so that we can convert them into a single number.
  4. Print the output.
				
					tup = (4,5,3,8)
str1 = ""
print("Original tuple: ",tup)
for val in tup:
    str1 += str(val)
num = int(str1)
print("Integer: ",num)
				
			

Output :

				
					Original tuple:  (4, 5, 3, 8)
Integer:  4538
				
			

convert a tuple of string values to a tuple of integer values.

compute the element-wise sum of tuples.

Convert a tuple of string values to a tuple of integer values

In this python tuple program, we will convert a tuple of string values to a tuple of integer values.

Steps to solve the program
  1. Take a tuple of string values as input.
  2. Create an empty list.
  3. Use for loop to iterate over each value from the tuple.
  4. Add the value to the list using append() after converting it to the integer using int().
  5. Print the output.
				
					tup = ('4563','68','1',)
print("Original tuple: ",tup)
l = []
for val in tup:
    if type(val) is str:
        l.append(int(val))
tup = tuple(l)
print("After converting to integers: ",tup)
				
			

Output :

				
					Original tuple:  ('4563', '68', '1')
After converting to integers:  (4563, 68, 1)
				
			

convert a string into a tuple.

convert a given tuple of integers into a number.

Python tuple program to convert a string into a tuple

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

Steps to solve the program
  1. Take a string as input.
  2. Now convert the string into a tuple using tuple().
  3. Print the output.
				
					str1 = "Sqatools"
print("String: ",str1)
tup = tuple(str1)
print("After converting string to a tuple: ",tup)
				
			

Output :

				
					String:  Sqatools
After converting string to a tuple:  ('S', 'q', 'a', 't', 'o', 'o', 'l', 's')
				
			

multiply all the elements in a tuple.

convert a tuple of string values to a tuple of integer values.

Python tuple program to multiply all the elements in a tuple

In this python tuple program, we will multiply all the elements in a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. Create a variable named product and assign its value equal to 1.
  3. Use a for loop to iterate over each number in the tuple.
  4. During iteration multiply the product variable with each number in the tuple.
				
					tup = (5,4,3,1)
print("Origianl tuple: ",tup)
product = 1
for val in tup:
    product *= val
print("Multiplication of elements in the tuple: ",product)
				
			

Output :

				
					Origianl tuple:  (5, 4, 3, 1)
Multiplication of elements in the tuple:  60
				
			

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

convert a string into a tuple.