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

Python: Saving lists that are outputs of a for loop?

I have a number of columns in an Excel spreadsheet (all different lengths) to import into Python, and I want to save each of these columns as a named list within Python. There are quite a few columns so I’d like to use a for loop to achieve this. I can import all of the columns and print them successfully during each iteration, but I can’t work out how I can save these lists under the name of the column? e.g. from column "1", I would like to end up with a list named "1" that consists of the 20 values in that column.

Apologies if this is a very simple question or there’s a better way of doing it – I’m still pretty new to Python.

Here’s what I’ve got so far that extracts and prints the columns as lists:

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

lists = 'SpreadsheetFilePath.xlsx'  
columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "6", "17", "18"]
    
All_Data = pd.read_excel(lists, sheet_name = 'Schedule')

for i in columns:
    newlist = All_Data[i].values.tolist()
    newerlist = [x for x in newlist if str(x) != 'nan']
    print(newerlist)

I essentially want to save newerlist under a list called "1" before the loop repeats.

Thanks in advance.

>Solution :

Maybe use a dictionary of lists, where keys are column names and values are lists:

values_by_column = {}
for i in columns:
    newlist = All_Data[i].values.tolist()
    newerlist = [x for x in newlist if str(x) != 'nan']
    values_by_column[i] = newerlist

print(values_by_column["1"])
print(values_by_column["2"])
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