![]() |
|
[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. 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 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
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“.
|