How to multiply all elements in list by constant in Python

Multiply all elements in list by constant in Python

Python is the best programming language for many developers. It is so easy that anyone can start learning without any previous programming experience. In this entire tutorial, you will learn how to multiply all elements in a list by constant in python using various methods.

Methods to Multiply all elements in the list by constant in Python

In this section, you will know all the best methods to multiply all elements in the list by constant using python language. Let’s get started.

Method 1: Multiplying elements using list comprehension

The first method to multiply all elements in the list by constant in python is List comprehension. List comprehension is used to generate a new list by iterating on the existing lists, tuples, strings, arrays e.t.c. The syntax for the list comprehension is below.

finalList = [ expression(i) for i in existingList if condition ]

Run the below lines of code to multiply all the elements in the list by some constant.

sample_list = [100, 200, 300, 400, 500]
new_list = [i * 10 for i in sample_list]
print(new_list)

You can see I have multiplied all the elements of the list by 10 by using the expression i * 10. Here ” i “  denotes each element of the list.

Output

Multiplying all elements of the list by constant in python using the list comprehension
Multiplying all elements of the list by constant in python using the list comprehension

Method 2: Multiply all elements in the list by constant in Python using for loop

The above method was using the list comprehension that is indirectly a loop. In this method, you will use it directly for  loop. It will iterate each element and multiply each item by the constant. In addition, it will append each multiplied result to the new list.

Execute the below lines of code to multiply all elements in the list by constant in python.

sample_list = [100, 200, 300, 400, 500]
new_list = []
for i in sample_list:
    new_list.append(i * 10)
print(new_list)

Output

Multiplying all elements of the list by constant in python using the for loop
Multiplying all elements of the list by constant in python using the for loop

Method 3: Multiplying using pandas

The third method to multiply elements in the list is through the pandas Python package. Pandas allow you to convert any list or other data structures into Dataframe and Series. In this example, you will first convert a list to a Series and multiply it by constant. After that, you will convert the Series to list by tolist() method.

Run the below lines of code to multiply each element.

import pandas as pd
sample_list = [100, 200, 300, 400, 500]
series = pd.Series(sample_list)
mul_series = series*10
print(mul_series.tolist())

Output

Multiplying all elements of the list by constant using pandas
Multiplying all elements of the list by constant using pandas

Method 4: Multiplying using NumPy

The above method was using the pandas package. The other method to multiply the elements of the list is using the NumPy python packages. You have to first convert the list to NumPy array and then multiply it by the constant. After that, you will convert the NumPy array to a list using the tolist() method.

import numpy as np
sample_list = [100, 200, 300, 400, 500]
numpy_array= np.array(sample_list)
mul_array = numpy_array * 10
print(mul_array.tolist())

Output

Multiplying all elements of the list by constant using numpy
Multiplying all elements of the list by constant using NumPy

Conclusion

These are the methods to multiply all the elements in the list in python. You can use all the methods in your code. But I will suggest you go with the fourth method which is using NumPy as its calculation is fast as compared to others.

I hope you have liked this tutorial and understood it. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner