Multiply List by Scalar in Python

Multiply List by Scalar in Python

  1. Multiply List Elements by a Scalar Using List Comprehensions in Python
  2. Multiply List Elements by a Scalar Using the map() Function in Python
  3. Multiply List Elements by a Scalar Using Lambda Functions in Python
  4. Multiply List Elements by a Scalar Using Numpy Arrays in Python

This tutorial will discuss how to multiply all the list elements with a scalar multiple in Python.

Multiply List Elements by a Scalar Using List Comprehensions in Python

List comprehensions are a way to perform operations on each list element without using a for loop in Python. List comprehensions are superior to loops because they have faster execution time and less code.

The following code snippet shows how to multiply all the list elements with a scalar quantity with list comprehensions in Python.

li = [1,2,3,4]
multiple = 2.5
li = [x*multiple for x in li]
print(li)

Output:

[2.5, 5.0, 7.5, 10.0]

In the above example, we multiplied our list li with a scalar multiple using the list comprehension [x*multiple for x in li]. After multiplying each element of the list, li with 2.5 is displayed in the output.

Multiply List Elements by a Scalar Using the map() Function in Python

The map() function is used to apply a user-defined function on each element of a particular iterable. The map() function takes the name of the method and iterable as input parameters and applies the method on each element of the iterable.

We’ll first have to define a method that performs multiplication on just one element to use this approach. This method will then be iteratively applied to each element of the list by the map() function.

The map() function returns a map object for each element and is converted into a list with the list() function in Python.

The following code snippet shows how we can use the map() function with a user-defined method to multiply all list elements with a scalar in Python.

li = [1,2,3,4]
multiple = 2.5
def multiply(le):
    return le*multiple

li = list(map(multiply,li))
print(li)

Output:

[2.5, 5.0, 7.5, 10.0]

We first defined the method multiply(le) that takes a single list element le as an input parameter, multiplies it with the scalar multiple, and returns the result. We then used the map(multiply, li) function to apply the multiply() on all elements of the list li.

The map() function is enclosed inside the list() function convert the results returned by the map() function into a list. In the end, the results after multiplying each element of the list li with 2.5 are displayed in the output.

Multiply List Elements by a Scalar Using Lambda Functions in Python

Lambda functions in Python are anonymous, one-liner functions that we only need to use once in our whole code. By anonymous, we mean that these functions have no name.

We can specify these functions in Python with the lambda keyword. We can use these lambda functions inside the previously discussed map() function to apply them to each list element.

The following code snippet shows how we can use lambda functions inside the map() function to multiply each list element by a scalar in Python.

li = [1,2,3,4]
multiple = 2.5
li = list(map(lambda x: x*multiple, li))
print(li)

Output:

[2.5, 5.0, 7.5, 10.0]

We specified a lambda function that multiplies each element of some iterable with the scalar multiple and applied it on the list li with the map() function. We converted the values returned by the map() function into a list() method in Python.

In the end, the results after multiplying each element of the list li with 2.5 are displayed in the output.

This method is much superior if compared to the previous method because we probably don’t need to use this function anywhere else in our code. Lambda functions are preferable over conventional ones when we need to use them only once in our code.

Multiply List Elements by a Scalar Using Numpy Arrays in Python

All the methods discussed previously perform operations on lists in Python. These methods work great when the list is small.

The only drawback of using these methods is that list operations are generally slower than NumPy arrays.

In this method, we first convert our list into a numpy array, multiply that array with a scalar, and then convert the resultant array back to a list with the tolist() function of NumPy arrays.

This method is preferable when the list size is huge and requires a lot of computation.

The following code snippet demonstrates the working of this method in Python.

import numpy as np
li = [1,2,3,4]
multiple = 2.5
arr = np.array(li)
arr = arr * multiple
li = arr.tolist()
print(li)

Output:

[2.5, 5.0, 7.5, 10.0]

We converted the list li into the array arr with the np.array(li) function. We then multiplied the array with the scalar multiple.

Unlike lists, we can easily multiply each element of a NumPy array with a scalar using arr = arr * multiple. After that, we converted our new array back into a list with the arr.tolist() function.

In the end, the results after multiplying each element of the list li with 2.5 are displayed in the output. This method should not be used when the list size is small.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python