[Python-ideas] Support multiplication for sets

Antoine Pitrou solipsis at pitrou.net
Fri Oct 7 13:07:48 CEST 2011


On Fri, 7 Oct 2011 11:46:34 +0100
Jakob Bowyer <jkbbwr at gmail.com> wrote:
> As far as I know and from asking my lecturer, multiplication only
> produces Cartesian products.

Given that multiplying a list or tuple repeats the sequence, there may
be a certain amount of confusion.
Also, I don't think itertools.product is common enough to warrant
an operator. There's a very readable alternative:

>>> a = {"amy", "martin"}
>>> b = {"smith", "jones", "john"}
>>> {(u, v) for u in a for v in b}
{('amy', 'john'), ('amy', 'jones'), ('martin', 'jones'), ('martin',
'smith'), ('martin', 'john'), ('amy', 'smith')}

Or, in the case where you only want to iterate, two nested loops will
suffice and avoid building the container.

Regards

Antoine.





More information about the Python-ideas mailing list