I need to create a nested tuple in which some values are the same, so I tried multiplying them.
t = ((('a', 'b'),) * 3)
print t
Prints
(('a', 'b'), ('a', 'b'), ('a', 'b'))
Is there a syntactically nicer way to do this?
Thanks.
The representation of that expression could be slightly simpler but it's almost canonical. I would use
t = 3 * (('a', 'b'), )
which removes one set of parentheses and visually warns about the multiplication before showing the tuple. You might imagine a further set of parentheses can be removed to give
t = 3 * ('a', 'b'),
but alas the result of this expression is not the required one, instead giving
(('a', 'b', 'a', 'b', 'a', 'b'),)
i.e. a tuple whose only element is the six generated elements.
(1 + 2) * 3, but in a context where they don't help disambiguate any grouping.