Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,560 questions

51,419 answers

573 users

How to find the product of elements between tuples in a dictionary by index with Python

3 Answers

0 votes
def productTuple(tpl):
    result = 1
    
    print(tpl)
    
    for item in tpl:
        result *= item
      
    return result

# Create a dictionary with tuples as values
dict = {'a': (1, 2, 3), 'b': (4, 5, 6), 'c': (7, 8, 9)}

print("Dictionary: " + str(dict))

# Calculate the product between the tuples elements
result = tuple(productTuple(element) for element in zip(*dict.values()))

print("Result: " + str(result))



'''
run:

Dictionary: {'a': (1, 2, 3), 'b': (4, 5, 6), 'c': (7, 8, 9)}
(1, 4, 7)
(2, 5, 8)
(3, 6, 9)
Result: (28, 80, 162)

'''

 



answered 2 days ago by avibootz
0 votes
def productTuple(tpl):
    result = 1
    
    print(tpl)
    
    for item in tpl:
        result *= item
      
    return result

# Create a dictionary with tuples as values
dict = {'a': (1, 2, 3), 'b': (4, 5, 6), 'c': (7, 8, 9)}

print("Dictionary: " + str(dict))

lst = [list(sub) for sub in dict.values()]
print("lst:", lst)

lst_tpl = [tuple(row[i] for row in lst) for i in range(len(lst[0]))]
print("lst_tpl:", lst_tpl)

result = tuple(map(productTuple, lst_tpl))
print("Result: " + str(result))


'''
run:

Dictionary: {'a': (1, 2, 3), 'b': (4, 5, 6), 'c': (7, 8, 9)}
lst: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
lst_tpl: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
(1, 4, 7)
(2, 5, 8)
(3, 6, 9)
Result: (28, 80, 162)

'''

 



answered 1 day ago by avibootz
0 votes
import operator
from functools import reduce

def productTuple(dict):
   return tuple(map(lambda *args: reduce(operator.mul, args), *dict.values()))

# Create a dictionary with tuples as values
dict = {'a': (1, 2, 3), 'b': (4, 5, 6), 'c': (7, 8, 9)}
print("Dictionary: " + str(dict))

result = productTuple(dict)
print("Result: " + str(result))


'''
run:

Dictionary: {'a': (1, 2, 3), 'b': (4, 5, 6), 'c': (7, 8, 9)}
Result: (28, 80, 162)

'''

 



answered 1 day ago by avibootz
...