# elementwise multiplication of 2 lists of numbers

Discussion in 'Python' started by harryos, Sep 20, 2010.

1. ### harryosGuest

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?

Any pointers most welcome,
harry
harryos, Sep 20, 2010

2. ### Gary HerronGuest

On 09/20/2010 07:02 AM, harryos wrote:
> 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?
>
> Any pointers most welcome,
> harry
>

List comprehension might be considered better by some, but that's a
subjective judgment. (One with which I agree.) List comprehension may
also be faster, but you'd have to test to know for sure.

>>> x=[2,4,3,1]
>>> y=[5,9,10,6]
>>> z = [a*b for a,b in zip(x,y)]
>>> print z

[10, 36, 30, 6]

Gary Herron
Gary Herron, Sep 20, 2010

3. ### Bruno DesthuilliersGuest

harryos a écrit :
> 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?

If it gives the expected results then it's at least *a* correct way !-)

> Or can this be done in a better way?

A list comp comes to mind, as well as using itertools.izip if your lists
are a bit on the huge side.

from itertools import izip
z = [a * b for a, b in izip(x, y)]
Bruno Desthuilliers, Sep 20, 2010
4. ### Giacomo BoffiGuest

harryos <> 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)
Giacomo Boffi, Sep 20, 2010
5. ### harryosGuest

On Sep 20, 7:28 pm, Bruno wrote:
>> A list comp comes to mind, as well as using itertools.izip

thanks Bruno,thanks Gary..
Should have thought of list comprehension..
Thanks for the pointer about izip

harry
harryos, Sep 20, 2010
6. ### Arnaud DelobelleGuest

On Sep 20, 3:02 pm, harryos <> wrote:
> 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?
>
> Any pointers most welcome,
> harry

In addition to what others have said, you can use the map builtin as
follows:
In Python 2.x:

>>> map(int.__mul__, [2, 4, 3, 1], [5, 9, 10, 6])

[10, 36, 30, 6]

If you don't know the list only contain ints use operator.mul.

In Python 3.x you need to wrap it in list() as map returns an
iterator.

HTH

--
Arnaud
Arnaud Delobelle, Sep 20, 2010
7. ### Peter OttenGuest

harryos wrote:

> 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?
>
> Any pointers most welcome,

Finally, if you have a lot of data that you want to process efficiently
consider using numpy arrays instead of lists:

>>> import numpy
>>> x = numpy.array([2,4,3,1])
>>> y = numpy.array([5,9,10,6])
>>> x*y

array([10, 36, 30, 6])

Peter
Peter Otten, Sep 20, 2010