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

Python, List Comprhension Multiplying 2D list by 1D list to each elements

I want to multiply a one dimensional list with a two dimensional list elements. How can I do it with list comprehension?

a = [1,2]
b = [[3,4],[5,6]]

The desired result is

c = [[3,8],[5,12]

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

>Solution :

You can use nested list comprehension:

c = [ [x * y for x, y in zip(a, row)] for row in 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