# elementwise multiplication of 2 lists of numbers

Giacomo Boffi giacomo.boffi at polimi.it
Mon Sep 20 16:39:34 CEST 2010

```harryos <oswald.harry at gmail.com> writes:

> hi
> I have 2 lists of numbers,say
> x=[2,4,3,1]
> y=[5,9,10,6]
> I need to create another list containing
> z=[2*5, 4*9, 3*10, 1*6]  ie =[10,36,30,6]
>
> I did not want to use numpy or any Array types.I tried to implement
> this in python .I tried the following
>
> z=[]
> for a,b in zip(x,y):
>         z.append(a*b)
> This gives me the correct result.Still,Is this the correct way?
> Or can this be done in a better way?

what you've done is correct, rather than in better ways this can be
done in different ways

first, there is list comprehension
>>> [x*y for x,y in zip([2,4,3,1],[5,9,10,6])]
[10, 36, 30, 6]
>>>

if you feel that "zip" looks like an artifact, python has some
functional bit
>>> map(lambda x,y: x*y, [2,4,3,1],[5,9,10,6])
[10, 36, 30, 6]
>>>

if you feel that "lambda" looks like an artifact,
>>> from operator import mul
>>> map(mul, [2,4,3,1],[5,9,10,6])
[10, 36, 30, 6]
>>>

hth,
--
> In tutti noi c'è un lato interista
Lato perlopiù nascosto dalle mutande.
--- Basil Fawlty, a reti unificate (IFQ+ISC)

```