Multiply a tuple by a constant

Peter Otten __peter__ at web.de
Fri Feb 13 11:41:10 CET 2004


Jay Davis wrote:

> I often need to multiply a tuple by a
> constant and the methods I use now are
> not very pretty.  The idea is to get a
> new tuple that is some factor times the
> start tuple, like  'newtup = .5 * oldtup'.
> 
> Is there a slick way to do this?

Not really slick, but it works:

>>> tuple(map(5 .__mul__, (1,2,3)))
(5, 10, 15)

The space after the 5 is necessary. Alternatively, you can wrap the constant
in brackets:

>>> tuple(map((5).__mul__, (1,2,3)))
(5, 10, 15)

Peter




More information about the Python-list mailing list