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).
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)
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
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
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
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)
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]
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]
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.
Source: Stackoverflow.com