Nadni
Nadni

Reputation: 178

Python: can't convert "None" or empty string values in a list

I have this list called query1:

[['PD', '', '', '', None, None, None, Decimal('43194'), Decimal('0'), Decimal('1000'), Decimal('500'), Decimal('67300'), Decimal('500'), Decimal('24426'), Decimal('2400'), Decimal('109700'), Decimal('1895'), Decimal('34000'), Decimal('100000'), None, None, Decimal('7900'), Decimal('1594'), None, None, Decimal('45900'), Decimal('12500'), None, Decimal('430'), Decimal('4550'), None, Decimal('11000'), Decimal('33700'), None, Decimal('10100'), Decimal('13500'), None, None, None, Decimal('96282'), Decimal('1000'), Decimal('2450'), Decimal('3700'), Decimal('19657'), Decimal('190700'), Decimal('7200'), Decimal('0'), Decimal('34650'), None, None, Decimal('1070'), None, Decimal('23200'), Decimal('200'), None, None, Decimal('42200'), Decimal('0'), Decimal('2000'), Decimal('7058'), None, Decimal('0'), None, None, None, Decimal('39900'), None, Decimal('21250'), Decimal('500'), Decimal('2000'), None, Decimal('4000'), Decimal('0'), Decimal('21900'), Decimal('349950'), Decimal('1200'), None], ['VE', '', '', '', None, None, None, Decimal('20700'), Decimal('0'), None, None, Decimal('65000'), Decimal('9300'), Decimal('8920'), Decimal('200'), Decimal('26500'), Decimal('2000'), Decimal('10850'), Decimal('73000'), None, None, Decimal('3000'), None, None, None, Decimal('11700'), Decimal('6000'), None, None, None, None, Decimal('500'), Decimal('300'), None, Decimal('1000'), Decimal('20000'), None, None, None, Decimal('7000'), None, Decimal('0'), Decimal('26500'), Decimal('5300'), Decimal('7000'), Decimal('3500'), None, Decimal('15000'), None, None, None, None, Decimal('13000'), Decimal('1000'), None, None, Decimal('19400'), Decimal('0'), None, Decimal('9900'), None, None, None, None, None, Decimal('32000'), None, Decimal('3000'), None, None, None, Decimal('3000'), Decimal('0'), Decimal('2000'), Decimal('18300'), None, None]]

And I want to change all the None and '' values to zeroes, but when I try this code:

    for i in range(len(query1)):
        for j in range(1, len(query1)):
            try:
                int(query1[i][j]) > 0
            except ValueError:
                query1[i][j] = 0

    print(query1)

this is the result I get:

[['PD', 0, '', '', None, None, None, Decimal('43194'), Decimal('0'), Decimal('1000'), Decimal('500'), Decimal('67300'), Decimal('500'), Decimal('24426'), Decimal('2400'), Decimal('109700'), Decimal('1895'), Decimal('34000'), Decimal('100000'), None, None, Decimal('7900'), Decimal('1594'), None, None, Decimal('45900'), Decimal('12500'), None, Decimal('430'), Decimal('4550'), None, Decimal('11000'), Decimal('33700'), None, Decimal('10100'), Decimal('13500'), None, None, None, Decimal('96282'), Decimal('1000'), Decimal('2450'), Decimal('3700'), Decimal('19657'), Decimal('190700'), Decimal('7200'), Decimal('0'), Decimal('34650'), None, None, Decimal('1070'), None, Decimal('23200'), Decimal('200'), None, None, Decimal('42200'), Decimal('0'), Decimal('2000'), Decimal('7058'), None, Decimal('0'), None, None, None, Decimal('39900'), None, Decimal('21250'), Decimal('500'), Decimal('2000'), None, Decimal('4000'), Decimal('0'), Decimal('21900'), Decimal('349950'), Decimal('1200'), None], ['VE', 0, '', '', None, None, None, Decimal('20700'), Decimal('0'), None, None, Decimal('65000'), Decimal('9300'), Decimal('8920'), Decimal('200'), Decimal('26500'), Decimal('2000'), Decimal('10850'), Decimal('73000'), None, None, Decimal('3000'), None, None, None, Decimal('11700'), Decimal('6000'), None, None, None, None, Decimal('500'), Decimal('300'), None, Decimal('1000'), Decimal('20000'), None, None, None, Decimal('7000'), None, Decimal('0'), Decimal('26500'), Decimal('5300'), Decimal('7000'), Decimal('3500'), None, Decimal('15000'), None, None, None, None, Decimal('13000'), Decimal('1000'), None, None, Decimal('19400'), Decimal('0'), None, Decimal('9900'), None, None, None, None, None, Decimal('32000'), None, Decimal('3000'), None, None, None, Decimal('3000'), Decimal('0'), Decimal('2000'), Decimal('18300'), None, None]]

I have also tried:

    for i in range(len(query1)):
        for j in range(1, len(query1)):
            if query1[i][j] is None or query[i][j] == 'None' or type(query[i][j]) is None or query1[i][j] == '':
                query[i][j] = 0
            else:
                query[i][j] = int(str(query[i][j]))

And the result is exactly the same. Any ideas on what I'm doing wrong?

Upvotes: 0

Views: 872

Answers (4)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48077

You may use nested List Comprehension here. Let's say your list is stored in variable as my_list, do

my_list = [[0 if item in [None, ''] else item for item in sub_list] for sub_list in my_list]

If it is ok to have Decimal('0'), instead of 0. You may do:

my_list = [[item or Decimal('0') for item in sub_list] for sub_list in my_list]

Upvotes: 2

pivanchy
pivanchy

Reputation: 723

So, the most Pythonic way to remove empty values(None, '', etc.) from list is filtering it:

test_list = ['something', None, '', False, 25, [1,2,3,4], (2,3)]
filtered_list = filter(None, test_list)
print filtered_list

['something', 25, [1, 2, 3, 4], (2, 3)]

If you have a list of lists, you may use filter in a single for loop(or to use a comprehension list)

Upvotes: 1

Manuel Taber
Manuel Taber

Reputation: 427

You have a list in a list, so you have to iterate over this two lists:

new_list = [] 
for outer_list_item in query1:
    temp_list = [] 
    for inner_list_item in outer_list_item:
        if inner_list_item in (None, ''):
            temp_list.append(0)
        else:
            temp_list.append(inner_list_item) 
    new_list.append(temp_list)

Upvotes: 2

Piotr Dabkowski
Piotr Dabkowski

Reputation: 5939

The problem with your code is in the second line. You are looping over length of main list.

Change it to:

for j in range(1, len(query1[i])):

and it should work. Also it's better to check the value with simple if statement:

if query[i][j] in (None, ''):
    query[i][j] = 0

You can also use list comprehension, but it's more advanced and it will take you some time to get used to it.

Upvotes: 2

Related Questions