How to multiply all integers inside list


Hello so I want to multiply the integers inside a list.

For example;

l = [1, 2, 3]
l = [1*2, 2*2, 3*2]


l = [2, 4, 6]

So I was searching online and most of the answers were regarding multiply all the integers with each other such as:


This question is tagged with python list multiplication scalar elementwise-operations

~ Asked on 2014-10-19 01:29:28

The Best Answer is


Try a list comprehension:

l = [x * 2 for x in l]

This goes through l, multiplying each element by two.

Of course, there's more than one way to do it. If you're into lambda functions and map, you can even do

l = map(lambda x: x * 2, l)

to apply the function lambda x: x * 2 to each element in l. This is equivalent to:

def timesTwo(x):
    return x * 2

l = map(timesTwo, l)

Note that map() returns a map object, not a list, so if you really need a list afterwards you can use the list() function afterwards, for instance:

l = list(map(timesTwo, l))

Thanks to Minyc510 in the comments for this clarification.

~ Answered on 2014-10-19 01:31:20


The most pythonic way would be to use a list comprehension:

l = [2*x for x in l]

If you need to do this for a large number of integers, use numpy arrays:

l = numpy.array(l, dtype=int)*2

A final alternative is to use map

l = list(map(lambda x:2*x, l))

~ Answered on 2014-10-19 01:34:02

Most Viewed Questions: