Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiply 2 lists

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]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading