SUMPROD over a set and a tuple

In the documentation of Python 3.12, it said that sumprod(p, q) returns the sum of products of values from two iterables p and q.

I tried the following example in IDLE, with a set and a tuple:

ptup = (1, 3, 5, 7)
qset = {2, 3, 5, 8}
math.sumprod(qset, ptup)
64

It is an error, since 1*2 + 3*3 + 5 * 5 + 7*8 != 64.

I tried the sumprod() with lists and also tuples and they work well. I tried to look up whether set is not an iterable object, but the answer I got is definitely a set is iterable.

So what is the problem here? Can we apply sumprod with set and list or set and tuple?

Please explain for me.

Thank you.

This is because a set does not have an order in Python, so even though you’ve written {2, 3, 5, 8} that could be stored in any order.

So in your case my bet is it is being stored as {8, 2, 3, 5} and then you get 1 * 8 + 3 * 2 + 5 * 3 + 7 * 5 = 64

Leave a Comment