[Tutor] multiply and sum two lists with list comprehension?

Lie Ryan lie.1296 at gmail.com
Thu Jan 28 12:13:20 CET 2010


On 01/28/10 17:22, Muhammad Ali wrote:
> Hi,
> 
> I am multipliying two lists so that each of list As elements get multiplied
> to the corresponding list Bs. Then I am summing the product.
> 
> For example, A= [1, 2, 3] and B=[2, 2, 2] so that I get [2, 4, 6] after
> multiplication and then sum it to get 12. I can do it using map like this:
> 
> sum(map(lambda i,j:i*j, A, B))
> 
> But map is being dropped out in python 3? or has it already been dropped?
> And I have heard Guido say that list comprehension do a better job than map
> so how would we do this with list comps?

No, `map` will not be dropped in the foreseeable future. `lambda`,
`map`, `filter`, and `reduce` will stay; but `reduce` will be moved into
functools module (from previously a built-in function).

http://www.artima.com/weblogs/viewpost.jsp?thread=98196



More information about the Tutor mailing list