Home > Back-end >  Append items in front to every list inside list of lists, not to the back
Append items in front to every list inside list of lists, not to the back

Time:12-24

I have the following list of lists.

list_list = 
[
 [1640, 0.1731, 0.173757, 0.1711825, 0.1726265, 4723572.914018], 
 [1640, 0.1726265, 0.1735915, 0.1715855, 0.1734615, 2590512.353352], 
 [1640, 0.1734615, 0.1750445, 0.1731025, 0.1739595, 1803995.1104755], 
]

I would like to append item_name to the front of the list such that it looks like this;

    item_name = "item"
    new_list_list = 
    [
     ["item", "item", 1640, 0.1731, 0.173757, 0.1711825, 0.1726265, 4723572.914018], 
     ["item", "item", 1640, 0.1726265, 0.1735915, 0.1715855, 0.1734615, 2590512.353352], 
     ["item", "item", 1640, 0.1734615, 0.1750445, 0.1731025, 0.1739595, 1803995.1104755], 
    ]

Here is the python code I wrote to do this;

item_name = "item"
new_list_list = [item   [item_name]   [item_name] for item in list_of_list] 

The code appends the items to the back of the list. What I want is append to the front of the list.

I am using python 3.9.

CodePudding user response:

try:

new_list_list = [[item_name]   [item_name]   item for item in list_of_list]

CodePudding user response:

This might do the trick:

new_list_list = [[item_name]   [item_name]   item for item in list_of_list] 
  • Related