Sick Gaming
[Tut] How to Multiply List Elements by a Number – Top 5 Ways - Printable Version

+- Sick Gaming (https://sickgaming.net)
+-- Forum: Programming (https://sickgaming.net/forum-76.html)
+--- Forum: Python (https://sickgaming.net/forum-83.html)
+--- Thread: [Tut] How to Multiply List Elements by a Number – Top 5 Ways (/thread-99704.html)



[Tut] How to Multiply List Elements by a Number – Top 5 Ways - xSicKxBot - 07-15-2022

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.

[Image: 1f4ac.png] 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
Code:
List Comprehension
to apply a mathematical operation to each element and return the result.

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

Code:
[4, 6, 10, 14, 22]



Method 2: Use Pandas tolist()

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

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

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

Code:
0 21 32 53 74 11dtype: int64

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

Code:
[4, 6, 10, 14, 22]

Method 3: Use map and lambda Functions

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

Code:
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 next line does the following:
  • The
    Code:
    map()
    function is passed the
    Code:
    lambda()
    function as an argument
    Code:
    (map(lambda x: x*2, prime_nums)
    ).

  • The
    Code:
    lambda
    performs the multiplication operation to each element of
    Code:
    prime_nums
    and saves it to
    Code:
    map()
    as an object similar to below.
    Code:
    <map object at 0x000001DC99CBBBB0>

  • The
    Code:
    map()
    object is then converted to a
    Code:
    List
    .

  • The results save to
    Code:
    mult_result
    .

Then,
Code:
mult_result
is output to the terminal.

Code:
[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()
function.

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

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

Code:
[4, 6, 10, 14, 22]



Method 5: Use Slicing

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

Code:
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_nums
.

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

Code:
[4, 6, 10, 14, 22]



[Image: 1f31f.png]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

[Image: 1f471-200d-2640-fe0f.png] Programmer 1: We have a problem
[Image: 1f9d4-200d-2642-fe0f.png] Programmer 2: Let’s use RegEx!
[Image: 1f471-200d-2640-fe0f.png] 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“. [Image: 1f642.png]