07-15-2022, 12:22 AM
How to Multiply List Elements by a Number – Top 5 Ways
5/5 – (1 vote)
Problem Formulation and Solution Overview
In this article, you’ll learn how to multiply List Elements by a Number in Python.
This example multiples the first five (5) Prime Numbers by two (2) and return the result.
Question: How would we write Python code to multiply the list elements?
We can accomplish this task by one of the following options:
Method 1: Use List Comprehension
This method uses to apply a mathematical operation to each element and return the result.
Above declares the first (5) Prime Numbers and saves this to . Next, loops through each element and applies the multiplication operation to each. The output saves to and is output to the terminal.
Method 2: Use Pandas tolist()
This method requires an additional library to be imported,, to use the function.
Above, imports the Library. Click here if this requires installation. Then, the first (5) Prime Numbers are declared and saved to .
Next, is passed as an argument to the function and returns . The output of at this point is shown below.
Now, we need to convert this output to a list () and apply the multiplication operation to each element. The results save to and are output to the terminal.
Method 3: Use map and lambda Functions
This method wraps the, and functions inside a Python and calculates the results.
Above declares the first (5) Prime Numbers and saves them to. The next line does the following:
Then, is output to the terminal.
Method 4: Use Numpy Array()
This method requires an additional library to be imported, NumPy, to use the function.
Above, imports the NumPy Library. Click here if this requires installation. Then the first (5) Prime Numbers are declared and saved to.
Next, is passed as an argument to where the multiplication operation is applied to each element. Then, this is converted to a List, saved to and output to the terminal.
Method 5: Use Slicing
This method uses Python’s infamous! No overhead, and a very pythonic way to resolve the issue.
Above declares the first (5) Prime Numbers and saves them to.
Then slicing is applied and used in conjunction with to apply the multiplication operation to each element. The results save back to and are output to the terminal.
A Finxter Favorite!
Summary
These methods of multiplying list elements by a number should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor
Programmer 1: We have a problem
Programmer 2: Let’s use RegEx!
Programmer 1: Now we have two problems
… yet – you can easily reduce the two problems to zero as you polish your “RegEx Superpower in Python“.
5/5 – (1 vote)
Problem Formulation and Solution Overview
In this article, you’ll learn how to multiply List Elements by a Number in Python.
This example multiples the first five (5) Prime Numbers by two (2) and return the result.
Question: How would we write Python code to multiply the list elements?We can accomplish this task by one of the following options:
- Method 1: Use Code:
List Comprehension
- Method 2: Use Code:
PandasCode:tolist()
- Method 3: Use and aCode:
map()Code:lambda()
- Method 4: Use NumPy Code:
Array()
- Method 5: Use Code:
Slicing
Method 1: Use List Comprehension
This method uses
Code:
List ComprehensionCode:
prime_nums = [2, 3, 5, 7, 11]
mult_result = [x * 2 for x in prime_nums]
print(mult_result)Above declares the first (5) Prime Numbers and saves this
Code:
ListCode:
prime_numsCode:
List ComprehensionCode:
mult_resultCode:
[4, 6, 10, 14, 22]Method 2: Use Pandas tolist()
This method requires an additional library to be imported,
Code:
PandasCode:
tolist()Code:
import pandas as pd prime_nums = [2, 3, 5, 7, 11]
mult_result = pd.Series(prime_nums)
mult_result = (mult_result*2).tolist()
print(mult_result)Above, imports the
Code:
PandasCode:
prime_numsNext,
Code:
prime_numsCode:
pd.Series()Code:
mult_resultCode:
mult_resultCode:
0 21 32 53 74 11dtype: int64Now, we need to convert this output to a list (
Code:
tolist()Code:
mult_resultCode:
[4, 6, 10, 14, 22]Method 3: Use map and lambda Functions
This method wraps the
Code:
map()Code:
lambdaCode:
ListCode:
prime_nums = [2, 3, 5, 7, 11]
mult_result = list(map(lambda x: x*2, prime_nums))
print(mult_result)Above declares the first (5) Prime Numbers and saves them to
Code:
prime_nums- The function is passed theCode:
map()function as an argumentCode:lambda()).Code:(map(lambda x: x*2, prime_nums)
- The performs the multiplication operation to each element ofCode:
lambdaand saves it toCode:prime_numsas an object similar to below.Code:map()
Code:<map object at 0x000001DC99CBBBB0>
- The object is then converted to aCode:
map().Code:List
- The results save to .Code:
mult_result
Then,
Code:
mult_resultCode:
[4, 6, 10, 14, 22]Method 4: Use Numpy Array()
This method requires an additional library to be imported, NumPy, to use the
Code:
np.array()Code:
import numpy as np prime_nums = [2, 3, 5, 7, 11]
the_result = list(np.array(prime_nums) * 2)
print(the_result)Above, imports the NumPy Library. Click here if this requires installation. Then the first (5) Prime Numbers are declared and saved to
Code:
prime_numsNext,
Code:
prime_numsCode:
np.array()Code:
the_resultCode:
[4, 6, 10, 14, 22]Method 5: Use Slicing
This method uses Python’s infamous
Code:
SlicingCode:
prime_nums = [2, 3, 5, 7, 11]
prime_nums[:] = [x * 2 for x in prime_nums]
print(prime_nums)Above declares the first (5) Prime Numbers and saves them to
Code:
prime_numsThen slicing is applied and used in conjunction with
Code:
List ComprehensionCode:
prime_numsCode:
[4, 6, 10, 14, 22]
A Finxter Favorite!Summary
These methods of multiplying list elements by a number should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor
Programmer 1: We have a problem
Programmer 2: Let’s use RegEx!
Programmer 1: Now we have two problems… yet – you can easily reduce the two problems to zero as you polish your “RegEx Superpower in Python“.

