Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Multiply List Elements by a Number – Top 5 Ways

#1
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 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]





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]





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]





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]




?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“. ?



https://www.sickgaming.net/blog/2022/07/...op-5-ways/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,051 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,973 08-26-2023, 01:28 AM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,956 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] [Fixed] Access Denied – OpenAI Error Reference Number 1020 xSicKxBot 0 1,419 08-10-2023, 09:24 PM
Last Post: xSicKxBot
  [Tut] [Fixed] Access Denied – OpenAI Error Reference Number 1020 xSicKxBot 0 1,389 05-25-2023, 04:45 PM
Last Post: xSicKxBot
  [Tut] F-String Python Hex, Oct, and Bin: Efficient Number Conversions xSicKxBot 0 1,669 03-28-2023, 12:01 PM
Last Post: xSicKxBot
  [Tut] Python | Split String by Number xSicKxBot 0 1,151 12-05-2022, 01:21 PM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,459 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] 6 Ways to Remove Python List Elements xSicKxBot 0 1,258 10-27-2022, 08:40 PM
Last Post: xSicKxBot
  [Tut] How to Count the Number of Unique Values in a List in Python? xSicKxBot 0 1,350 10-19-2022, 03:08 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016