Python

Python Multiply List by Scalar

Working with lists is one of the most important concepts in the Python programming language and it is also a fundamental concept as well. Learning to multiply a complete list with a scalar value is good practice for not only a new programmer but also for a moderate-level programmer.

This post will guide you on how to multiply a list by a scalar in python, and the content for this post is as:

Multiply List by Scalar Using List Comprehension

List comprehension is a technique in which a new list is formed by performing an action on every item of an iterable (list, tuple, array, map) one by one. To better understand the list comprehension method, take a look at its syntax

Syntax

returnedList = [ x * scalar for x in mainList ]

In the above syntax:

  • returnedList” is the last that will be created as a result of the list comprehension
  • x” is the variable in which every item of the list will be passed to for the multiplication
  • scalar” is a numerical constant
  • for-in” is the iterator that will place the value of the items in the list to the variable “x”

Example: Multiply a List Using List Comprehension

First create a list using the following line:

demoList = [3, 6 , 9, 12, 15, 18, 21]

After that, create a new list “resultList” by using the list comprehension and set the scalar value to 5:

resultList = [x * 5 for x in demoList]

After that, print the “resultList”:

print(resultList)

Upon executing this code, the following result will be displayed on the terminal:

The whole list has been multiplied by a scalar value. In case you do not want to create a new list and want to optimize the code, then in that case, use the following lines of code:

demoList = [3, 6 , 9, 12, 15, 18, 21]

demoList = [x * 5 for x in demoList]

print(demoList)

This will produce the following result:

In this way, you have multiplied the same list with a scalar value without creating a new list.

Multiply List by Scalar Using Loops in Python

The second to multiply the list by a scalar value is by using a loop and then multiplying each individual element with the scalar value inside the body of the loop.

To test this, use the following lines code:

demoList = [3, 6 , 9, 12, 15, 18, 21]

for i in range(0, len(demoList)):
    demoList[i] = demoList[i] * 3 #Scalar value
   
print(demoList)

In the above code snippet:

  • Create a list variable “demoList”.
  • Initialize a for loop and start it from 0 up to the length of the list by using the len() method.
  • For every iteration, take the element at the “i” index and multiply it by a scalar value and place the result back at the “i” index.
  • Print the list outside of the loop

Upon the execution of the above code snippet, the following result will be displayed on the terminal:

The output confirms that the list has successfully been multiplied by a scalar value

Multiply List by Scalar Using the Numpy Library

In this method, the list is first converted into an array using the Numpy library and then multiplied by a scalar value. After that, the array is again converted back to a list to get the result that the user wants. To use this, first import the numpy library into your Python program and create the list:

import numpy as np

demoList = [3, 6 , 9, 12, 15, 18, 21]

Convert the list into a numpy array and multiply it with a number:

array = np.array(demoList)

array = array * 3

Convert the array back to a list by using the toList() method and then print out the list on the terminal using the following lines:

demoList = array.tolist()

print(demoList)

The complete code snippet is as:

import numpy as np
demoList = [3, 6 , 9, 12, 15, 18, 21]

array = np.array(demoList)
array = array * 3
demoList = array.tolist()

print(demoList)

Running the above code will produce the following output on the terminal:

The list has been successfully multiplied with a scalar

Multiply List by Scalar Using the map() Method

To use the map() method to multiply all the values of a list, first create a function using the following lines of code:

demoList = [3, 6 , 9, 12, 15, 18, 21]

def listMultiple(item):
    value = 6
    return item*value

The function “listMultiple” takes in an attribute and multiplies it with a value which is set to “6”. The next step is to use the map() method to send every item of the list to this function and then store the result at the same index of the map, and at the end convert the map into a list using the list() method:

demoList = list(map(listMultiple,demoList))

Once that is done, print the list on the terminal:

print(demoList)

Complete code snippet or using map() method to multiply a list by scalar is:

demoList = [3, 6 , 9, 12, 15, 18, 21]

def listMultiple(item):
    value = 6
    return item*value

demoList = list(map(listMultiple,demoList))

print(demoList)

This code will produce the following results on the terminal:

The list has been multiplied with a scalar successfully.

Conclusion

To multiply a list with a scalar in python, the user can use various ways which include using the Numpy library, the map() method, loops in python, and list comprehension. All of these ways are elaborated on with examples in this post. But there are still many ways the same feat can be achieved with the help of external modules that can be downloaded into your Python environment.

About the author

Admin

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.