Home > Mobile >  Multiply 2 lists
Multiply 2 lists

Time:04-26

I want to multiply two lists and then create a dictionary; my problem is that my code gives me a one-row list of results, but I want to separate results by two (because I have 2 items in 'num_list').

num_list = [2323875.123,
            18063259.91]
           

percents = [0.056725468,
0.032356829,
0.031189631,
0.029635805,
0.025242697,
0.023660115,
0.020755944,
0.020504972
]

arr = [item * percent for item in num_list for percent in percents]

CodePudding user response:

You need another pair of square brackets:

[[item * percent for percent in percents] for item in num_list]

You may also want to use NumPy: faster and more convenient.

import numpy as np
a = np.array(num_list)
b = np.array(percents)
result = np.outer(a, b)
  • Related