hrisger
hrisger

Reputation: 1

consecutively multiplying the elements of a list with another list python

I'm new to python and my problem is most likely very easily solved, but I couldn't figure it out and I couldn't find any topics that matched my specific issue. I have 2 lists of numbers in python: Eg. a=[0.01,0.02,0.03,0.04] b=[0.02,0.03,0.04,0.05] I would like to multiply every element in list "a" with all the elements from list "b" and produce ,in this case, 4 new lists: a0=a[0]*b a1=a[1]*b a2=a[2]*b a3=a[3]*b What is the best way to do that?

Upvotes: 0

Views: 62

Answers (2)

Mikael
Mikael

Reputation: 554

Does this do what you want? Hope it helps :)

for element in a:
    for i in range(len(b)):
        b[i] = b[i] * element

Upvotes: -1

Liteye
Liteye

Reputation: 2801

It can be [[x * y for y in b] for x in a]

Or [x * y for x in a for y in b] if you want a flattened result

Upvotes: 2

Related Questions