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.

Leave a Comment