2

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
  • 31,849
  • 12
  • 86
  • 121
  • 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

1

You can use nested list comprehension:

c = [ [x * y for x, y in zip(a, row)] for row in b ]
trincot
  • 317,000
  • 35
  • 244
  • 286