multiply each element of a list by a number

Martin martin at marcher.name
Fri Dec 26 20:29:39 EST 2008


you might also want to look into the map(elem), and filter(elem) builtins


>>> def multby3(elem):
...     return elem * 3
...
>>> map(multby3, (1, 2, 3, ))
[3, 6, 9]
>>> help(map)
>>> def even(elem):
...     return not elem % 2
...
>>> filter(even, (1, 2, 3, ))
(2,)
>>> help(filter)
KeyboardInterrupt
>>> map(multby3, filter(even, (1, 2, 3, )))
[6]
>>>

hth
martin

2008/12/27 Scott David Daniels <Scott.Daniels at acm.org>:
> 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]


-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html



More information about the Python-list mailing list