How to Multiply List Elements by a Number – Top 5 Ways

5/5 - (5 votes)

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 List Comprehension to apply a mathematical operation to each element and return the result.

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 List to prime_nums. Next, List Comprehension loops through each element and applies the multiplication operation to each. The output saves to mult_result and is output to the terminal.

[4, 6, 10, 14, 22]
Python One-Liner Trick 9 - Nested List Comprehension

Method 2: Use Pandas tolist()

This method requires an additional library to be imported, Pandas, to use the tolist() function.

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 Pandas Library. Click here if this requires installation. Then, the first (5) Prime Numbers are declared and saved to prime_nums.

Next, prime_nums is passed as an argument to the pd.Series() function and returns mult_result. The output of mult_result at this point is shown below.

0 2
1 3
2 5
3 7
4 11
dtype: int64

Now, we need to convert this output to a list (tolist()) and apply the multiplication operation to each element. The results save to mult_result and are output to the terminal.

[4, 6, 10, 14, 22]

Method 3: Use map and lambda Functions

This method wraps the map(), and lambda functions inside a Python List and calculates the results.

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 prime_nums. The next line does the following:

  • The map() function is passed the lambda() function as an argument (map(lambda x: x*2, prime_nums)).
  • The lambda performs the multiplication operation to each element of prime_nums and saves it to map() as an object similar to below.
    <map object at 0x000001DC99CBBBB0>
  • The map() object is then converted to a List.
  • The results save to mult_result.

Then, mult_result is output to the terminal.

[4, 6, 10, 14, 22]
Let's Play Finxter - The Lambda Function in Python

Method 4: Use Numpy Array()

This method requires an additional library to be imported, NumPy, to use the np.array() function.

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 prime_nums.

Next, prime_nums is passed as an argument to np.array() where the multiplication operation is applied to each element. Then, this is converted to a List, saved to the_result and output to the terminal.

[4, 6, 10, 14, 22]
NumPy Tutorial - Everything You Need to Know to Get Started

Method 5: Use Slicing

This method uses Python’s infamous Slicing! No overhead, and a very pythonic way to resolve the issue.

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 prime_nums.

Then slicing is applied and used in conjunction with List Comprehension to apply the multiplication operation to each element. The results save back to prime_nums and are output to the terminal.

[4, 6, 10, 14, 22]
The Ultimate Guide to Slicing in Python

🌟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“. πŸ™‚