Electric_Sheep01
Electric_Sheep01

Reputation: 117

Python can't remove None value from list of lists

I'm trying to remove a None value from a csv file I have. I have converted blank values to None values in the first part of the below code but in the last part when I envoke filter It prints the column_list but the None values remain also. I need to remove them so I can work out max/min values of each which doesn't appear to work with them in the list?

with (inFile) as f:
    _= next(f)
    list_of_lists = [[float(i) if i.strip() != '' else None for i in line.split(',')[2:]]
        for line in f]
inFile.close()

log_GDP_list = [item[0] for item in list_of_lists]
social_support_list = [item[1] for item in list_of_lists]
healthy_birth_list = [item[2] for item in list_of_lists]
freedom_choices_list = [item[3] for item in list_of_lists]
generosity_list = [item[4] for item in list_of_lists]
confidence_gov_list = [item[5] for item in list_of_lists]

column_list = []
column_list.append([log_GDP_list, social_support_list, healthy_birth_list, freedom_choices_list, generosity_list, confidence_gov_list])
res = list(filter(None, column_list)) 
print(res)

Also, when running the filter on just one of the row lists (such as log_GDP_list) it removes the None values but I still get an error saying I can't run max() or min() on floats (all values were converted from strings to floats in the first bit of the code).

Upvotes: 0

Views: 257

Answers (1)

Tom Dalton
Tom Dalton

Reputation: 6190

You currently have something like this

l = [
    float(i) if i.strip() != '' else None
    for i in line.split(',')[2:]
]

what you want is this:

l = [
    float(i)
    for i in line.split(',')[2:]
    if i.strip()
]

This way, when i.strip() evaluates to False, the item wont be added to the resulting list at all.

Upvotes: 1

Related Questions