70

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

For example;

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

output:

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:

[1*2*3]

roadrunner66
  • 7,772
  • 4
  • 32
  • 38
Andre
  • 1,601
  • 6
  • 19
  • 19

6 Answers6

111

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.

APerson
  • 8,140
  • 8
  • 35
  • 49
  • ahh i see it does a loop. Is there any way more direct than a loop ? ( i dont know if that made sense :p) – Andre Oct 19 '14 at 01:32
  • 3
    @Ali : Although a list comprehension uses a loop it's a very efficient kind of loop, so a list comprehension is generally much faster than using an explicit `for` loop. – PM 2Ring Oct 19 '14 at 02:10
  • Note that using map with lambda functions will return a map object (an iterable). You will need to cast the result of map to a list to get the desired result. Like so: l = list(map(lambda x : x*2, l)) – kebab-case Feb 18 '20 at 16:42
27

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))
Joshua
  • 2,431
  • 15
  • 23
6

Another functional approach which is maybe a little easier to look at than an anonymous function if you go that route is using functools.partial to utilize the two-parameter operator.mul with a fixed multiple

>>> from functools import partial
>>> from operator import mul
>>> double = partial(mul, 2)
>>> list(map(double, [1, 2, 3]))
[2, 4, 6]
miradulo
  • 28,857
  • 6
  • 80
  • 93
6

The simplest way to me is:

map((2).__mul__, [1, 2, 3])
blameless75
  • 2,148
  • 2
  • 19
  • 14
5

using numpy :

    In [1]: import numpy as np

    In [2]: nums = np.array([1,2,3])*2

    In [3]: nums.tolist()
    Out[4]: [2, 4, 6]
0
#multiplying each element in the list and adding it into an empty list
original = [1, 2, 3]
results = []
for num in original:
    results.append(num*2)# multiply each iterative number by 2 and add it to the empty list.

print(results)
salparadise
  • 5,699
  • 1
  • 26
  • 32
  • 1
    @MirzaSisic basically there are so many ways to go about solving the above problem. i chose to create an empty list(results = []) so after each iteration i can add(append) the iterated number multiplied by 2 to the empty list. and finally print out the empty list which contains the iterated numbers multiplied by 2. if you still don't get it please point to specifics so i can help you. thanks – Jonathan Akwetey Okine Nov 28 '17 at 00:57
  • 1
    @salparadise thumbs up for the change in variable names. – Jonathan Akwetey Okine Nov 28 '17 at 01:01