3

I want to multiply a one dimensional list with a two dimensional list's 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]

I have tried this way

c = [i*j for i, j in zip(a,b)]

The error that I encountered was

TypeError: can't multiply sequence by non-int of type 'list'

Makyen
  • 29,855
  • 12
  • 76
  • 115
  • Does this answer your question? [List comprehension on a nested list?](https://stackoverflow.com/questions/18072759/list-comprehension-on-a-nested-list) – Braiam Feb 03 '22 at 18:04

1 Answers1

2

You can use nested list comprehension:

c = [ [x * y for x, y in zip(a, row)] for row in b ]
trincot
  • 263,463
  • 30
  • 215
  • 251