[python] How to multiply all integers inside list

Hello so I want to multiply the integers inside a list.

For example;

l = [1, 2, 3]
l = [1*2, 2*2, 3*2]

output:

l = [2, 4, 6]

So I was searching online and most of the answers were regarding multiply all the integers with each other such as:

[1*2*3]

The answer is


using numpy :

    In [1]: import numpy as np

    In [2]: nums = np.array([1,2,3])*2

    In [3]: nums.tolist()
    Out[4]: [2, 4, 6]

#multiplying each element in the list and adding it into an empty list
original = [1, 2, 3]
results = []
for num in original:
    results.append(num*2)# multiply each iterative number by 2 and add it to the empty list.

print(results)

The simplest way to me is:

map((2).__mul__, [1, 2, 3])

The most pythonic way would be to use a list comprehension:

l = [2*x for x in l]

If you need to do this for a large number of integers, use numpy arrays:

l = numpy.array(l, dtype=int)*2

A final alternative is to use map

l = list(map(lambda x:2*x, l))

Another functional approach which is maybe a little easier to look at than an anonymous function if you go that route is using functools.partial to utilize the two-parameter operator.mul with a fixed multiple

>>> from functools import partial
>>> from operator import mul
>>> double = partial(mul, 2)
>>> list(map(double, [1, 2, 3]))
[2, 4, 6]

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to list

Convert List to Pandas Dataframe Column Python find elements in one list that are not in the other Sorting a list with stream.sorted() in Java Python Loop: List Index Out of Range How to combine two lists in R How do I multiply each element in a list by a number? Save a list to a .txt file The most efficient way to remove first N elements in a list? TypeError: list indices must be integers or slices, not str Parse JSON String into List<string>

Examples related to multiplication

How to multiply all integers inside list How can I multiply all items in a list together with Python? Why does multiplication repeats the number several times? How to perform element-wise multiplication of two lists? How to multiply individual elements of a list with a number? Is multiplication and division using shift operators in C actually faster? How can a query multiply 2 cell for each row MySQL? Create list of single item repeated N times How can I multiply and divide using only bit shifting and adding?

Examples related to scalar

How to multiply all integers inside list TypeError: only length-1 arrays can be converted to Python scalars while trying to exponentially fit data Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index" python: how to identify if a variable is an array or a scalar how to create and call scalar function in sql server 2008 Python RuntimeWarning: overflow encountered in long scalars PHP - cannot use a scalar as an array warning PHP Constants Containing Arrays?

Examples related to elementwise-operations

How to get element-wise matrix multiplication (Hadamard product) in numpy? How to multiply all integers inside list Element-wise addition of 2 lists? Comparing two NumPy arrays for equality, element-wise How to perform element-wise multiplication of two lists?