I was hoping for an elegant or effective way to multiply sequences of integers (or floats).
My first thought was to try (1, 2, 3) * (1, 2, 2)
would result (1, 4, 6)
, the products of the individual multiplications.
Though python isn't preset to do that for sequences. Which is fine, I wouldn't really expect it to. So what's the pythonic way to multiply (or possibly other arithmetic operations as well) each item in two series with and to their respective indices?
A second example (0.6, 3.5) * (4, 4)
= (2.4, 14)
Concatenating and Multiplying Tuples Operators can be used to concatenate or multiply tuples. Concatenation is done with the + operator, and multiplication is done with the * operator.
To multiply the elements of a tuple: Use a generator expression to iterate over the tuple. Multiply each tuple element by a number. Use the tuple() class to convert the result to a tuple.
Multiply Two Lists in Python Using the numpy. multiply() Method. The multiply() method of the NumPy library in Python, takes two arrays/lists as input and returns an array/list after performing element-wise multiplication.
When it is required to concatenate multiple tuples, the '+' operator can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error.
Tuple multiplication in Python. When it is required to perform tuple multiplication, the 'zip' method and the generator expression can be used. The zip method takes iterables, aggregates them into a tuple, and returns it as the result. Generator is a simple way of creating iterators.
In python, to multiply two equal length lists we will use zip () to get the list and it will multiply together and then it will be appended to a new list.
Tuple Length To determine how many items a tuple has, use the len()method: Example Print the number of items in the tuple: thistuple = ("apple", "banana", "cherry") print(len(thistuple)) Try it Yourself »
In python, element-wise multiplication can be done by importing numpy. To multiply two equal-length arrays we will use np.multiply() and it will multiply element-wise. Example: import numpy as np m1 = [3, 5, 1] m2 = [2, 1, 6] print(np.multiply(m1, m2))
The simplest way is to use zip
function, with a generator expression, like this
tuple(l * r for l, r in zip(left, right))
For example,
>>> tuple(l * r for l, r in zip((1, 2, 3), (1, 2, 3)))
(1, 4, 9)
>>> tuple(l * r for l, r in zip((0.6, 3.5), (4, 4)))
(2.4, 14.0)
In Python 2.x, zip
returns a list of tuples. If you want to avoid creating the temporary list, you can use itertools.izip
, like this
>>> from itertools import izip
>>> tuple(l * r for l, r in izip((1, 2, 3), (1, 2, 3)))
(1, 4, 9)
>>> tuple(l * r for l, r in izip((0.6, 3.5), (4, 4)))
(2.4, 14.0)
You can read more about the differences between zip
and itertools.izip
in this question.
A simpler way would be:
from operator import mul
In [19]: tuple(map(mul, [0, 1, 2, 3], [10, 20, 30, 40]))
Out[19]: (0, 20, 60, 120)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With