Home > front end >  How to I append elements to a list of lists in python
How to I append elements to a list of lists in python

Time:11-04

I've a list of lists like:

a = [[1,2], [3,4]]

To each of the lists I want to add an element like 0 at the beginning, so that the result looks like:

b = [[0,1,2], [0,3,4]]

What's the Python way to do it? I already tried it with "map", but without success. I know, I could easily do it with a loop, but what's the functional way?

CodePudding user response:

Use list comprehension and addition of lists to get in done in one short line of code:

b = [[0] item for item in a]
  • Related