469,327 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

list*list

There must be a better way to multiply the elements of one list by
another:

a = [1,2,3]
b = [1,2,3]
c = []
for i in range(len(a)):
c.append(a[i]*b[i])
a = c
print a
[1, 4, 9]

Perhaps a list comprehension or is this better addressed by NumPy?

Thanks,

jab

May 1 '06 #1
10 1199
> There must be a better way to multiply the elements of one list by
another:

a = [1,2,3]
b = [1,2,3]
c = []
for i in range(len(a)):
c.append(a[i]*b[i])
a = c
print a
[1, 4, 9]

Perhaps a list comprehension or is this better addressed by NumPy?

First of all: it's considered bad style to use range if all you want is a
enumeration of indices, as it will actually create a list of the size you
specified. Use xrange in such cases.

You can use a listcomp like this:

c = [a[i] * b[i] for i in xrange(len(a))]

But maybe nicer is zip:

c = [av * bv for av, bv in zip(a, b)]

And if lists get large and perhaps multidemnsional, numpy certainly is the
way to go.

Diez

May 1 '06 #2
> There must be a better way to multiply the elements of one list by
another:

a = [1,2,3]
b = [1,2,3]
c = []
for i in range(len(a)):
c.append(a[i]*b[i])
a = c
print a
[1, 4, 9]

Perhaps a list comprehension or is this better addressed by NumPy?


a = [1,2,3]
b = [1,2,3]
c = [q*r for q,r in zip(a,b)]

seems to do the trick for me.

-tim


May 1 '06 #3
"Diez B. Roggisch" <de***@nospam.web.de> wrote in message
news:4b*************@uni-berlin.de...
it's considered bad style to use range if all you want is a
enumeration of indices, as it will actually create a list of the size you
specified. Use xrange in such cases.


I'm pretty sure this distinction goes away in 2.5.
Cheers,
Alan Isaac
May 1 '06 #4
David Isaac wrote:
it's considered bad style to use range if all you want is a
enumeration of indices, as it will actually create a list of the size you
specified. Use xrange in such cases.


I'm pretty sure this distinction goes away in 2.5.


3.0.

and for short sequences, it doesn't really matter much in the 2.X series.
it's definitely not "bad style" to use range instead of xrange for a ten to,
say, 1000 item loop.

(it's not always the most efficient thing to do, though, but that's
another story).

</F>

May 1 '06 #5
> and for short sequences, it doesn't really matter much in the 2.X series.
it's definitely not "bad style" to use range instead of xrange for a ten
to, say, 1000 item loop.


You and me are aware of that - but I figured the OP wasn't. And just the
other day somebody on de.c.l.py wondered about the memory consumption of
his little script that contained a range(9000000) which he used for a loop.

And as in the case of a "for in" one can't even access the list produced by
range() and thus there is virtually no difference to xrange, I'd consider
it a code smell if it's used there - why creating an object you don't use
when you can't even get a reference to it?

Diez
May 1 '06 #6
Diez wrote:
First of all: it's considered bad style to use range if all you want is a
enumeration of indices, as it will actually create a list of the size you
specified. Use xrange in such cases. But maybe nicer is zip:
c = [av * bv for av, bv in zip(a, b)]


By your logic, shouldn't it also be "bad style" to create an
unnecessary list with zip instead of using izip?

-Mike

May 1 '06 #7
Klaas schrieb:
Diez wrote:
First of all: it's considered bad style to use range if all you want is a
enumeration of indices, as it will actually create a list of the size you
specified. Use xrange in such cases.

But maybe nicer is zip:
c = [av * bv for av, bv in zip(a, b)]


By your logic, shouldn't it also be "bad style" to create an
unnecessary list with zip instead of using izip?


Yep. I always forget about the itertools. And then of course we'd go for
an generator expression, won't we?
Diez
May 1 '06 #8
BBands wrote:
There must be a better way to multiply the elements of one list by
another:
[snipped]
Perhaps a list comprehension or is this better addressed by NumPy?
If you have a large amount of numerical code, it is definetly better to
use numpy, since it is intended just for that purpose:
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([1, 2, 3])
c = a * b
c array([1, 4, 9])

Otherwise, you can use the builtin function map and functions in the
operator module:
import operator
a = [1, 2, 3]
b = [1, 2, 3]
c = map(operator.mul, a, b)
c [1, 4, 9] d = map(operator.add, a, b)
d

[2, 4, 6]
Thanks,

jab


Ziga

May 1 '06 #9
Very useful comments... Thanks to all!

Once again this community has demonstrated why Python is THE language.

jab

May 3 '06 #10
Ziga Seilnach:
c = map(operator.mul, a, b)


Usually I like map a lot, but this time for me the l.c. version is a
bit simpler to understand (even if it's longer, and maybe slower too):
from operator import mul
from itertools import izip
a = [1, 2, 3]
b = [4, 5, 6]
map(mul, a, b) [4, 10, 18] [arow * brow for arow, brow in izip(a, b)]

[4, 10, 18]

Bye,
bearophile

May 3 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by massimo | last post: by
24 posts views Thread by Robin Cole | last post: by
4 posts views Thread by JS | last post: by
3 posts views Thread by chellappa | last post: by
reply views Thread by drewy2k12 | last post: by
reply views Thread by Atos | last post: by
12 posts views Thread by kalyan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.