Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: make nested list based on elements on single list [closed]

Tags:

python

I have a list of strings:

ls = ['elev', 'solRd']

I want to create a a new list, with nested list of two elements, where the second element actually better explains the meaning of the first one.

ls.out = [["elev", "elevation"], ["solRd", "solRadiation"]]

I have only few strings, which will repeat, so I would like to specify it manually.

Ie. if element is 'elev' -> new pair item will be 'elevation'; if element is 'solRd'-> new element 'solarRadiation', etc.

This seems pretty easy but I am relatively new to python and I cannot figure it our.

I have tried to subset my element by name ls['a'] and include it to new list, but even the subsetting by name dis not worked.. I don't want to subset it by index, in case my string order will change.

like image 391
maycca Avatar asked Aug 03 '26 03:08

maycca


1 Answers

meanings = {
    "elev": "elevation",
    "solRd": "solRadiation"
}

ls = ["elev", "solRd"]

lists = [[item, meanings.get(item, "")] for item in ls]
like image 97
Paul M. Avatar answered Aug 04 '26 16:08

Paul M.