133

I have a list:

my_list = [1, 2, 3, 4, 5]

How can I multiply each element in my_list by 5? The output should be:

[5, 10, 15, 20, 25]
Alexander
  • 105,104
  • 32
  • 201
  • 196
DJ bigdawg
  • 1,489
  • 2
  • 9
  • 6
  • 2
    Using `map` is better than `for-loop`. – Tony Feb 03 '16 at 02:04
  • 9
    [And list comprehension is (almost always) better than `map`](https://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map) (and always better if `map` would require a `lambda`). – ShadowRanger Feb 03 '16 at 02:21

10 Answers10

188

You can just use a list comprehension:

my_list = [1, 2, 3, 4, 5]
my_new_list = [i * 5 for i in my_list]

>>> print(my_new_list)
[5, 10, 15, 20, 25]

Note that a list comprehension is generally a more efficient way to do a for loop:

my_new_list = []
for i in my_list:
    my_new_list.append(i * 5)

>>> print(my_new_list)
[5, 10, 15, 20, 25]

As an alternative, here is a solution using the popular Pandas package:

import pandas as pd

s = pd.Series(my_list)

>>> s * 5
0     5
1    10
2    15
3    20
4    25
dtype: int64

Or, if you just want the list:

>>> (s * 5).tolist()
[5, 10, 15, 20, 25]

Finally, one could use map, although this is generally frowned upon.

my_new_list = map(lambda x: x * 5, my_list)

Using map, however, is generally less efficient. Per a comment from ShadowRanger on a deleted answer to this question:

The reason "no one" uses it is that, in general, it's a performance pessimization. The only time it's worth considering map in CPython is if you're using a built-in function implemented in C as the mapping function; otherwise, map is going to run equal to or slower than the more Pythonic listcomp or genexpr (which are also more explicit about whether they're lazy generators or eager list creators; on Py3, your code wouldn't work without wrapping the map call in list). If you're using map with a lambda function, stop, you're doing it wrong.

And another one of his comments posted to this reply:

Please don't teach people to use map with lambda; the instant you need a lambda, you'd have been better off with a list comprehension or generator expression. If you're clever, you can make map work without lambdas a lot, e.g. in this case, map((5).__mul__, my_list), although in this particular case, thanks to some optimizations in the byte code interpreter for simple int math, [x * 5 for x in my_list] is faster, as well as being more Pythonic and simpler.

Alexander
  • 105,104
  • 32
  • 201
  • 196
  • Variables name which starts with caps isn't Pythonic. And they're lists, not numbers. So I'd suggest use `l1` and `l2` as the variable name. – Remi Guan Feb 03 '16 at 02:34
  • 5
    The use of 'l' as a variable is also discouraged because the letter 'l' and the number 1 are easily confused. I used the variables in the OP's original question, and I believe your edit of the question did more harm than good. – Alexander Feb 03 '16 at 02:39
  • 1
    If you feel my edit is bad, you can edit the question to improve it. Also, we can choose other variable rather than `l1` such as `l_1`, `list_1`, etc. These are all better than `Num_1`. – Remi Guan Feb 03 '16 at 03:16
36

A blazingly faster approach is to do the multiplication in a vectorized manner instead of looping over the list. Numpy has already provided a very simply and handy way for this that you can use.

>>> import numpy as np
>>> 
>>> my_list = np.array([1, 2, 3, 4, 5])
>>> 
>>> my_list * 5
array([ 5, 10, 15, 20, 25])

Note that this doesn't work with Python's native lists. If you multiply a number with a list it will repeat the items of the as the size of that number.

In [15]: my_list *= 1000

In [16]: len(my_list)
Out[16]: 5000

If you want a pure Python-based approach using a list comprehension is basically the most Pythonic way to go.

In [6]: my_list = [1, 2, 3, 4, 5]

In [7]: [5 * i for i in my_list]
Out[7]: [5, 10, 15, 20, 25]

Beside list comprehension, as a pure functional approach, you can also use built-in map() function as following:

In [10]: list(map((5).__mul__, my_list))
Out[10]: [5, 10, 15, 20, 25]

This code passes all the items within the my_list to 5's __mul__ method and returns an iterator-like object (in python-3.x). You can then convert the iterator to list using list() built in function (in Python-2.x you don't need that because map return a list by default).

benchmarks:

In [18]: %timeit [5 * i for i in my_list]
463 ns ± 10.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [19]: %timeit list(map((5).__mul__, my_list))
784 ns ± 10.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [20]: %timeit [5 * i for i in my_list * 100000]
20.8 ms ± 115 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [21]: %timeit list(map((5).__mul__, my_list * 100000))
30.6 ms ± 169 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)


In [24]: arr = np.array(my_list * 100000)

In [25]: %timeit arr * 5
899 µs ± 4.98 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
AndyG
  • 39,700
  • 8
  • 109
  • 143
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • I'm interested in why the numpy method is 'blazingly faster'? Could you possibly elaborate or point me towards some resources? I'm not sure what vectorization is. – Aerinmund Fagelson Apr 14 '20 at 09:11
  • 1
    @AerinmundFagelson Here -> https://stackoverflow.com/questions/35091979/why-is-vectorization-faster-in-general-than-loops – Mazdak Apr 14 '20 at 09:19
  • @Kasramvd That link is incorrect. The link discusses a different meaning of _vectorization_ as single-instruction-multiple-data (applying an operation to many data at once, as GPUs do). In the context of NumPy, _vectorization_ refers to using fast pre-compiled C loops to operate on a data sequence, rather than pure Python. – xjcl Jun 05 '20 at 18:22
  • [Here's an accurate link](https://www.pythonlikeyoumeanit.com/Module3_IntroducingNumpy/VectorizedOperations.html#Vectorized-Operations) – xjcl Jun 05 '20 at 18:27
20

You can do it in-place like so:

 l = [1, 2, 3, 4, 5]
 l[:] = [x * 5 for x in l]

This requires no additional imports and is very pythonic.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67
  • In addition: the concept is called _list comprehension_ if you want to look up more information about it. – Michael Feb 03 '16 at 01:00
  • 2
    I'd rather `l = [x * 5 for x in l]` over `l[:] = [x * 5 for x in l]`. The latter creates a new list, then uses it to overwrite the contents of `l` as opposed to just reassigning the reference which is cheaper. If you're actually worried about space, just iterate with a loop and mutate in-place. – cs95 Feb 20 '20 at 23:59
6

Since I think you are new with Python, lets do the long way, iterate thru your list using for loop and multiply and append each element to a new list.

using for loop

lst = [5, 20 ,15]
product = []
for i in lst:
    product.append(i*5)
print product

using list comprehension, this is also same as using for-loop but more 'pythonic'

lst = [5, 20 ,15]

prod = [i * 5 for i in lst]
print prod
ellaRT
  • 1,346
  • 2
  • 16
  • 39
  • How is the "long way" in any way better? It's longer—thus more difficult to read—and not any easier to write. – lirtosiast Feb 03 '16 at 05:59
  • 1
    okay maybe you got the wrong impression on my title and i never said it was any better, just trying to show him how to do it without using comprehension. because on my experience when i was new in python i wasn't able to grasp the concept of comprehension easily. – ellaRT Feb 03 '16 at 06:07
  • Oh, okay. I can't directly relate because I started off with functional languages. – lirtosiast Feb 03 '16 at 06:09
  • I see, well there. I edited the answer to also include the efficient way of doing it. – ellaRT Feb 03 '16 at 06:11
4

With map (not as good, but another approach to the problem):

list(map(lambda x: x*5,[5, 10, 15, 20, 25]))

also, if you happen to be using numpy or numpy arrays, you could use this:

import numpy as np
list(np.array(x) * 5)
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
3
from functools import partial as p
from operator import mul
map(p(mul,5),my_list)

is one way you could do it ... your teacher probably knows a much less complicated way that was probably covered in class

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • You can do it without the import statements using a lambda expression. Also, your snippet returns a map object, which is useless unless cast to a list. list(map(lambda x: 5*x, my_list)). – castle-bravo Feb 03 '16 at 01:10
  • @castle-bravo its usefulness depends on what you need to do with it ... there are many ways of accomplishing this solution (as I mention ...) – Joran Beasley Feb 03 '16 at 01:16
  • 4
    Please don't teach people to use `map` with `lambda`; the instant you need a `lambda`, you'd have been better off with a list comprehension or generator expression. If you're clever, you _can_ make `map` work without `lambda`s a lot, e.g. in this case, `map((5).__mul__, my_list)`, although in this particular case, thanks to some optimizations in the byte code interpreter for simple `int` math, `[x * 5 for x in my_list]` is faster, as well as being more Pythonic and simpler. – ShadowRanger Feb 03 '16 at 02:18
2

Multiplying each element in my_list by k:

k = 5
my_list = [1,2,3,4]
result = list(map(lambda x: x * k, my_list))

resulting in: [5, 10, 15, 20]

Vityata
  • 42,633
  • 8
  • 55
  • 100
0

I found it interesting to use list comprehension or map with just one object name x. Note that whenever x is reassigned, its id(x) changes, i.e. points to a different object.

x = [1, 2, 3]
id(x)
2707834975552
x = [1.5 * x for x in x]
id(x)
2707834976576
x
[1.5, 3.0, 4.5]
list(map(lambda x : 2 * x / 3, x))
[1.0, 2.0, 3.0]
id(x) # not reassigned
2707834976576
x = list(map(lambda x : 2 * x / 3, x))
x
[1.0, 2.0, 3.0]
id(x)
2707834980928
Leon Chang
  • 669
  • 8
  • 12
0
var1 = [2,4,6,8,10,12]

#for Integer multiplier us   int(x).__mull__ with map    
var2 = list( map(int(2).__mul__,var1 ))
#for float multiplier
var2 = list( map(float(2.5).__mul__,var1 ))
-1

Best way is to use list comprehension:

def map_to_list(my_list, n):
# multiply every value in my_list by n
# Use list comprehension!
    my_new_list = [i * n for i in my_list]
    return my_new_list
# To test:
print(map_to_list([1,2,3], -1))

Returns: [-1, -2, -3]

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • This just takes the accepted answer and turns it into a function. You could probably do that with more than half of the responses on SO, but it doesn't add anything and is not what the OP asked. – Alexander Feb 10 '20 at 19:20