Convert a tuple into a list by adding the string after every element of the tuple

In this python tuple program, we will convert a tuple into a list by adding the string after every element of the tuple.

Steps to solve the program
  1. Take a tuple and a string as input.
  2. Create an empty list.
  3. Use a for loop to iterate over elements in the tuple.
  4. Add the element and the string to the empty list using append().
  5. Repeat the process for all elements.
  6. Print the output.
				
					a = (1,2,3,4)
print("Tuple: ",a)
b= 'sqatools'
print("String: ",b)
l = []
for ele in a:
    l.append(ele)
    l.append(b)
				
			

Output :

				
					Tuple:  (1, 2, 3, 4)
String:  sqatools
New list:  [1, 'sqatools', 2, 'sqatools', 3, 'sqatools', 4, 'sqatools']
				
			

flatten a tuple list into a string.

Program to flatten a list of tuples into a string

In this python tuple program, we will flatten a list of tuples into a string.

Steps to solve the program
  1. Take a list of tuples as input and create an empty string.
  2. Use a for loop to iterate over the tuples in the list.
  3. Use a nested for loop to iterate over characters in the tuples.
  4. Add the characters to the string.
  5. Print the output.
				
					l = [('s','q','a'),('t','o'),('o','l','s')]
print("Original list of tuples: ",l)
str1 = ""
for tup in l:
    for char in tup:
        str1 += char+" "
print("New string: ",str1)
				
			

Output :

				
					Original list of tuples:  [('s', 'q', 'a'), ('t', 'o'), ('o', 'l', 's')]
New string:  s q a t o o l s 
				
			

flatten a list of lists into a tuple.

convert a tuple into a list by adding the string after every element of the tuple.

Program to flatten a list of lists into a tuple

In this python tuple program, we will flatten a list of lists into a tuple.

Steps to solve the program
  1. Take a list of lists as input and create an empty list.
  2. Use a for loop to iterate over the list in the lists.
  3. Use a nested for loop to iterate over elements in each list.
  4. Add elements to the empty list using append().
  5. Convert the new list to a tuple using tuple().
  6. Print the output.
				
					l = [['s'],['q'],['a'],['t'],['o'],['o'],['l'],['s']]
print("List of lists: ",l)
l1 = []
for lists in l:
    for char in lists:
        l1.append(char)
tup = tuple(l1)
print("Tuple: ",tup)
				
			

Output :

				
					List of lists:  [['s'], ['q'], ['a'], ['t'], ['o'], ['o'], ['l'], ['s']]
Tuple:  ('s', 'q', 'a', 't', 'o', 'o', 'l', 's')
				
			

multiply ith element from each tuple from a list of tuples.

flatten a tuple list into a string.

Multiply ith element from each tuple from a list of tuples

In this python tuple program, we will multiply ith element from each tuple from a list of tuples.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Create a variable i and product set their value equal to 1.
  3. Use a for loop to iterate over tuples in the list.
  4. Use a nested for loop to iterate over elements in the tuples.
  5. Check if the index of the element in the tuple is equal to i using index().
  6. If the index is equal then multiply the product variable with that element of the tuple.
  7. Print the output.
				
					l = [(4,8,3),(3,4,0),(1,6,2)]
print("Original list of tuples: ",l)
i = 1
product = 1
for tup in l:
    for ele in tup:
        if tup.index(ele) == i:
            product *= ele
print(f"Product of {i} element in each tuple is {product}")
				
			

Output :

				
					Original list of tuples:  [(4, 8, 3), (3, 4, 0), (1, 6, 2)]
Product of 1 element in each tuple is 192
				
			

extract digits from a list of tuples.

flatten a list of lists into a tuple.

Program to extract digits from a list of tuples

In this python tuple program, we will extract digits from a list of tuples.

Steps to solve the program
				
					import re
l = [(6,87,7),(4,53),(11,28,3)]
print("Original list of tuples: ",l)
temp = re.sub(r'[\[\]\(\), ]', '', str(l))
result = [int(ele) for ele in set(temp)]
print("Digits in the list of tuples: ",result)
				
			

Output :

				
					Original list of tuples:  [(6, 87, 7), (4, 53), (11, 28, 3)]
Digits in the list of tuples:  [6, 8, 5, 4, 7, 3, 1, 2]
				
			

remove duplicates from a tuple.

multiply ith element from each tuple from a list of tuples.

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.