list*list

Tim Chase python.list at tim.thechases.com
Mon May 1 11:51:28 EDT 2006


> 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







More information about the Python-list mailing list