multiply each element of a list by a number

Scott David Daniels Scott.Daniels at Acm.Org
Fri Dec 26 20:05:58 EST 2008


Tim Chase wrote:
>> What does *not* work is         3 * [0,1,2]
>> As you know, this gives
>>         [0,1,2,0,1,2,0,1,2]
>> What I am hoping for is
>>         [0,3,6]
>> I see that I can use         numpy.multiply(3,range(3))
> 
> The common way to do this is just
>  a1 = [0,1,2]
>  a2 = [x * 3 for x in a1]
...

But a specifically Numpy kind of answer is:

     import numpy
     a = numpy.array([0, 1, 2])
     print a * 3


-Scott



More information about the Python-list mailing list