In this python tuple program, we will multiply ith element from each tuple from a list of tuples.
Steps to solve the program
- Take a list of tuples as input.
- Create a variable i and product set their value equal to 1.
- Use a for loop to iterate over tuples in the list.
- Use a nested for loop to iterate over elements in the tuples.
- Check if the index of the element in the tuple is equal to i using index().
- If the index is equal then multiply the product variable with that element of the tuple.
- 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