Last updated: Apr 9, 2024
Reading timeยท7 min
To multiply each element in a list by a number:
# โ Multiply each element in a list by a number import math my_list = [2, 4, 6] result = [item * 10 for item in my_list] print(result) # ๐๏ธ [20, 40, 60] # ------------------------------------------ # โ Multiply all elements in a list my_list = [2, 4, 6] result = math.prod(my_list) print(result) # ๐๏ธ 48
We used a list comprehension to iterate over the list and multiplied each list
item by 10
.
On each iteration, we multiply the current list item by the specified number and return the result.
Alternatively, you can use a simple for loop.
for
loopThis is a four-step process:
for
loop to iterate over the original list.my_list = [2, 4, 6] result = [] for item in my_list: result.append(item * 10) print(result) # ๐๏ธ [20, 40, 60]
The for
loop works in a very similar way to the list comprehension, but
instead of returning the list items directly, we append them to a new list.
map()
You can also use the map()
function to multiply each element in a list.
my_list = [2, 4, 6] result = list(map(lambda item: item * 10, my_list)) print(result) # ๐๏ธ [20, 40, 60]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
map
gets called with each item in the list, multiplies the item by 10
and returns the result.The last step is to use the list()
class to convert the map
object to a
list
.
If you work with NumPy arrays, you can directly use the multiplication operator on the array to multiply each of its elements by a number.
import numpy as np arr = np.array([2, 4, 6]) result = arr * 10 print(result) # ๐๏ธ [20 40 60]
Note that this only works with NumPy arrays. If you multiply a Python list by a number, it gets repeated N times.
print([2, 4, 6] * 2) # ๐๏ธ [2, 4, 6, 2, 4, 6]
Multiplying a Python list by N returns a new list containing the elements of the original list repeated N times.
If you need to multiply all elements in a list, use the math.prod()
function.
The math.prod()
method calculates the product of all the elements in the
provided iterable.
import math my_list = [2, 3, 5] result = math.prod(my_list) print(result) # ๐๏ธ 30
Make sure to import the math
module at the top.
We used the math.prod
method to multiply all the elements in a list.
The math.prod() method calculates the product of all the elements in the provided iterable.
import math my_list = [5, 5, 5] result = math.prod(my_list) print(result) # ๐๏ธ 125
The method takes the following 2 arguments:
Name | Description |
---|---|
iterable | An iterable whose elements to calculate the product of |
start | The start value for the product (defaults to 1 ) |
If the iterable is empty, the start
value is returned.
Alternatively, you can use the reduce()
function.
reduce()
This is a two-step process:
reduce()
function.from functools import reduce my_list = [2, 3, 5] result = reduce(lambda x, y: x * y, my_list) print(result) # ๐๏ธ 30
The reduce() function takes the following 3 parameters:
Name | Description |
---|---|
function | A function that takes 2 parameters - the accumulated value and a value from the iterable. |
iterable | Each element in the iterable will get passed as an argument to the function. |
initializer | An optional initializer value that is placed before the items of the iterable in the calculation. |
If we provide a value for the initializer
argument, it is placed before the
items of the iterable in the calculation.
from functools import reduce my_list = [2, 3, 5] def do_math(acc, curr): print(acc) # ๐๏ธ is 10 on first iteration return acc * curr result = reduce(do_math, my_list, 10) print(result) # ๐๏ธ 300
We passed 10
for the initializer argument, so the value of the accumulator
will be 10
on the first iteration.
accumulator
would get set to the first element in the iterable if we didn't pass a value for the initializer
.If the iterable
is empty and the initializer
is provided, the initializer
is returned.
If the initializer
is not provided and the iterable contains only 1
item,
the first item is returned.
from functools import reduce my_list = [2] result = reduce(lambda acc, curr: acc * curr, my_list) print(result) # ๐๏ธ 2
for
loopYou can also use a for
loop to multiply all elements in a list.
my_list = [2, 3, 5] result = 1 for item in my_list: result = result * item print(result) # ๐๏ธ 30
On each iteration of the for
loop, we multiply the result
variable by the
current list item and reassign it to the result.
To multiply two lists element-wise:
zip
function to get an iterable of tuples with the corresponding
items.list_1 = [1, 2, 3] list_2 = [4, 5, 6] # ๐๏ธ [(1, 4), (2, 5), (3, 6)] print(list(zip(list_1, list_2))) result = [x * y for x, y in zip(list_1, list_2)] print(result) # ๐๏ธ [4, 10, 18]
The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.
list_1 = [1, 2, 3] list_2 = [4, 5, 6] # ๐๏ธ [(1, 4), (2, 5), (3, 6)] print(list(zip(list_1, list_2)))
You can imagine that the zip()
function iterates over the lists, taking 1 item
from each.
0
, the second tuple consists of the elements in each list that have an index of 1
, etc.The last step is to use a list comprehension to iterate over the zip
object
and multiply the values in each tuple.
list_1 = [1, 2, 3] list_2 = [4, 5, 6] # ๐๏ธ [(1, 4), (2, 5), (3, 6)] print(list(zip(list_1, list_2))) result = [x * y for x, y in zip(list_1, list_2)] print(result) # ๐๏ธ [4, 10, 18]
On each iteration, we unpack the values from the tuple and use the
multiplication *
operator to multiply them.
a, b = (2, 5) print(a * b) # ๐๏ธ 10
You can also use this approach to multiply more than two lists element-wise.
list_1 = [1, 2, 3] list_2 = [4, 5, 6] list_3 = [7, 8, 9] # ๐๏ธ [(1, 4, 7), (2, 5, 8), (3, 6, 9)] print(list(zip(list_1, list_2, list_3))) result = [x * y * z for x, y, z in zip(list_1, list_2, list_3)] print(result) # ๐๏ธ [28, 80, 162]
Alternatively, you can use the map()
function.
map()
This is a two-step process:
map()
function to call the mul()
function with the two lists.list()
class to
convert the map object to a list.from operator import mul list_1 = [1, 2, 3] list_2 = [4, 5, 6] result = list(map(mul, list_1, list_2)) print(result) # ๐๏ธ [4, 10, 18]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
The mul
function from the operator
module is the same as a * b
.
map
calls the mul
function with each item of the two iterables (e.g. items at index 0
, then 1
, etc).The map
function returns a map
object, so we had to use the list()
class
to convert the result to a list.
You can learn more about the related topics by checking out the following tutorials: