Table of Contents

- Multiply operator in Python
- Multiplying a string with an integer
- Multiplying all the elements of a list
- Multiply elements of a list with another number
- Using the for loop to multiply the elements of a list with another number
- Using the list comprehension method to multiply the elements of a list with another number
- Using the map() function to multiply the elements of a list with another number
- Using the numpy.array() function to multiply the elements of a list to another number

- Multiplying a list with another list

Python allows us to perform various mathematical operations using different operators and functions. Multiplication is one of the four basic arithmetic operations.

## Multiply operator in Python

To perform multiplication in Python, we use the `*`

operator.

**For example,**

1 2 3 4 5 6 |
a = 5 b = 4 c = a * b print(c) |

Output:

The final output is an integer as well. Similarly, we can multiply two float values as well.

For example,

1 2 3 4 5 6 |
a = 5.55 b = 4.3 c = a * b print(c) |

Output:

The final result here is a float value. We can control the final number of digits after the decimal place using the round() function or string formatting. If we multiply an integer and a float value, then also we get a float value in the result.

See the code below.

1 2 3 4 5 6 |
a = 5.55 b = 4 c = a * b print(c) |

Output:

In this article, we will further discuss multiplication in Python.

## Multiplying a string with an integer

If we multiply a string with an integer, it will duplicate that string by the number given.

See the code below.

1 2 3 |
print('a'*5) |

Output:

However, if we have a string containing a number and wish to perform normal multiplication with it, we can typecast it to an integer using the `int()`

function. After this, we can perform normal multiplication using the `*`

operator.

For example,

1 2 3 4 5 6 |
s = '10' a = 5 c = int(s)*a print(c) |

Output:

## Multiplying all the elements of a list

A list is used to store different elements under a common name. We can perform multiplication operations on a list also.

First, we will discuss different methods to multiply all the elements within a list.

### Use the `for`

loop to multiply all the elements of a list

We can iterate through the list using the `for`

loop, multiply each element with one individually, and store the product in a separate variable.

For example,

1 2 3 4 5 6 7 |
lst = [7,5,2] c = 1 for i in lst: c = c*i print(c) |

Output:

In the above example, we initialized the variable `c`

with the value 1. We multiplied this with every element of the list and displayed its final value.

## Further reading:

### Use the `functools.reduce()`

function to multiply all the elements of a list

The `functools.reduce()`

function is used to apply a given function to elements of the iterable. It takes a given number of elements at a time, stores their result, and applies the same function on this list for the remaining elements.

For example,

1 2 3 4 5 6 |
from functools import reduce lst = [7,5,2] c = reduce(lambda x, y: x*y, lst) print(c) |

Output:

In the above example, we used the `lambda`

keyword to create a single line function, which multiplies two elements from the list at a time.

In Python 3 and above, the `reduce()`

function was shifted to the `functools`

module. However, we do not need to import any module to use this function in Python 2. If we want to make something compatible with both the versions of Python, we can use the `reduce()`

function from the `six`

module, which is compatible with both Python 2 and Python 3.

### Use the `numpy.prod()`

function to multiply all the elements of a list

The `numpy.prod()`

function returns the product of all the elements in an array over some specified axis. We can use this method for lists also.

For example,

1 2 3 4 5 6 |
import numpy as np lst = [7,5,2] c = np.prod(lst) print(c) |

Output:

### Use the `math.prod()`

function to multiply all the elements of a list

The `math.prod()`

function works similar to the above method, but it works with all iterables. This is an addition to the recent versions of Python.

For example,

1 2 3 4 5 6 |
import math lst = [7,5,2] c = math.prod(lst) print(c) |

Output:

## Multiply elements of a list with another number

We can also multiply all the elements of a list by a given number. We cannot directly use the `number*list`

expression because it will not provide the required result and will duplicate the list the specified number of times, like a string.

Thus, we will discuss other methods on how to multiply all the elements of a list with another number.

### Using the `for`

loop to multiply the elements of a list with another number

We can iterate through the loop, multiply each element individually, and then store the result in a new list.

For example,

1 2 3 4 5 6 7 |
lst = [7,5,2] c = [] for i in lst: c.append(5*i) print(c) |

Output:

In the above example, we multiplied all the elements of the list by 5 and stored the new elements in a new list.

### Using the list comprehension method to multiply the elements of a list with another number

We do the same thing in the previous example, but using list comprehension. This is a convenient and elegant way to create lists using a single line of code.

See the code below.

1 2 3 4 5 |
lst = [7,5,2] c = [5*i for i in lst] print(c) |

Output:

### Using the `map()`

function to multiply the elements of a list with another number

The `map()`

function can apply a given function to all the elements of an iterable. It returns a `map`

-type object in recent versions of Python.

To multiply the elements of a list with a number, we can use this method. We will provide the `__mul__`

function, which will multiply all the elements with the required number.

For example,

1 2 3 4 5 |
lst = [7,5,2] c = list(map((5).__mul__,lst)) print(c) |

Output:

We use the `list()`

function, in the end, to convert the `map`

type object to a list.

### Using the `numpy.array()`

function to multiply the elements of a list to another number

If we multiply a number with an array, it will multiply all the elements of the array with it. The same does not happen with a list, as discussed earlier.

For example,

1 2 3 4 5 |
lst = [7,5,2] c = list(np.array(lst)*5) print(c) |

Output:

Let us discuss what happened in the above example.

- We used the
`numpy.array()`

function to convert the list to an array. - We performed the necessary multiplication using the
`*`

operator. - Converted the result back to a list using the
`list()`

function.

## Multiplying a list with another list

We can multiply every element of a list by the corresponding elements of another list. In our examples, we will assume both the list to be of the same length.

### Using the `for`

loop to multiply a list with another list

In this method, we will iterate through the length of the list, and multiply every element at the corresponding index. We will append the result to a new list.

For example,

1 2 3 4 5 6 7 8 9 |
lst1 = [5,1,4] lst2 = [2,3,1] lst3 = [] for i in range(len(lst1)): c = lst1[i] * lst2[i] lst3.append(c) print(lst3) |

Output:

We can use the list comprehension method to achieve the same in a single line of code.

See the code below.

1 2 3 4 5 6 |
lst1 = [5,1,4] lst2 = [2,3,1] lst3 = [lst1[i]*lst2[i] for i in range(len(lst1))] print(lst3) |

Output:

### Using the `numpy`

module to multiply a list with another list

We can use two methods from the `numpy`

module to achieve this. The first is by using the `numpy.multiply()`

function. This function works with lists and arrays, and multiplies every element from one list with the corresponding element at the other list. The final result will be stored in an array.

For example,

1 2 3 4 5 6 7 |
import numpy as np lst1 = [5,1,4] lst2 = [2,3,1] lst3 = np.multiply(lst1,lst2) print(lst3) |

Output:

We can also convert the lists to an array and multiply them. The `numpy.array()`

function will convert them to an array, and then we can multiply them using the `*`

operator.

For example,

1 2 3 4 5 6 7 8 |
import numpy as np lst1 = [5,1,4] lst2 = [2,3,1] a = np.array(lst1) b = np.array(lst2) print(a*b) |

Output:

We can convert the final type back to a list using the `list()`

function in both of the above-mentioned methods.

### Using the `map()`

function to multiply a list with another list

As discussed earlier, the `map()`

function can be used to apply a function to elements of an iterable. We can use it with two iterables and multiply their elements.

For example,

1 2 3 4 5 6 7 |
import operator lst1 = [5,1,4] lst2 = [2,3,1] lst3 = list(map(operator.mul,lst1,lst2)) print(lst3) |

Output:

In the above example,

- The
`operator.mul()`

function essentially multiplies two numbers. - This function is applied to the corresponding elements of both lists.
- The
`list()`

function type-casts the final object to a list.

In this section, we discussed many methods on how to perform multiplication operations in a list. We can use most of these methods for other iterables. Some methods might be compatible with arrays, for others, we can convert them to a list using the `tolist()`

function and use the above-mentioned methods.

That’s all about how to multiply in Python.