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

Append items in front to every list inside list of lists, not to the back

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;

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

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.

>Solution :

try:

new_list_list = [[item_name] + [item_name] + item for item in list_of_list]
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