Multiply a tuple by a constant

Larry Bates lbates at swamisoft.com
Fri Feb 13 16:56:17 CET 2004


two ways:

t=(1,2,3)
newt=tuple(map(lambda x: x*0.5, t))

Note: apparently the map function is slated for deprication
(to be replaced by list comprehension)

List comprehension:

t=(1,2,3)
newt=tuple([x*0.5 for x in t])

-Larry Bates
-----------------------------------------------------------
"Jay Davis" <dj00302003 at yahoo.com> wrote in message
news:1d17eeb7.0402130220.7258a5bd at posting.google.com...
> 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?





More information about the Python-list mailing list