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

Updating False values in a list based on the previous iteration

I have a list of lists:

['col1', False, False, False, False, False]
['col1', 'col2', False, False, False, False]
['col1', False, 'col3a', False, False, False]
['col1', False, 'col3b', False, False, False]
['col1', False, False, 'col4', False, False]
['col1', False, False, 'col4', False, False]

As I go through each row, I’m looking to see if the current item is False and if the item in the previous row (same pos) is not False, I’ll replace the current item with the previous one.

So the new list of lists would be:

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

list_of_lists = [
    ['col1', False, False, False, False, False],
    ['col1', 'col2', False, False, False, False],
    ['col1', 'col2', 'col3a', False, False, False],
    ['col1', 'col2', 'col3b', False, False, False],
    ['col1', 'col2', 'col3b', 'col4', False, False],
    ['col1', 'col2', 'col3b', 'col4', False, False],
]

Here is my code:

for row_num in range(len(list_of_lists)):
    display_list = []
    if row_num == 0:
        continue
    for col_num in range(len(list_of_lists[row_num])):
        current_row = list_of_lists[row_num][col_num]
        previous_row = list_of_lists[row_num - 1][col_num]
        if current_row == False and previous_row != False:
            display_list.append(previous_row)
        else:
            display_list.append(current_row)
    print(display_list)

It incorrectly outputs:

['col1', False, False, False, False, False]
['col1', 'col2', False, False, False, False]
['col1', 'col2', 'col3a', False, False, False]
['col1', False, 'col3b', False, False, False]
['col1', False, 'col3b', 'col4', False, False]
['col1', False, False, 'col4', False, False]

Why is it only revising two values? If a False value was replaced, in the next iteration, it seems like it does not know it changed? For example:

['col1', 'col2', 'col3a', False, False, False] 
['col1', False, 'col3b', False, False, False]

Why does 1st item in the 2nd list not know 'col2' was updated, thus use it to replace the False in its position?

>Solution :

Here, when we calculate the previous value, I’m taking it from the display_list instead of the list_of_lists (since display_list would now have the updated values)

list_of_lists = [
    ['col1', False, False, False, False, False],
    ['col1', 'col2', False, False, False, False],
    ['col1', False, 'col3a', False, False, False],
    ['col1', False, 'col3b', False, False, False],
    ['col1', False, False, 'col4', False, False],
    ['col1', False, False, 'col4', False, False]
]

display_list = [[val for val in list_of_lists[0]]] # add the first row
for row_num in range(1, len(list_of_lists)): # from row 2 to end
    tmp_row = [] # for temporarily saving the updated row
    for col_num in range(len(list_of_lists[row_num])):
        current_cell = list_of_lists[row_num][col_num] # get current row from list_of_lists
        previous_cell = display_list[row_num - 1][col_num] # get previous row from display_list
        if current_cell is False and previous_cell: # pytonic way to check for falsy or truthy value
            tmp_row.append(previous_cell)
        else:
            tmp_row.append(current_cell)
    display_list.append(tmp_row) # append the tmp_row

for row in display_list:
    print(row)

This results in following result

['col1', False, False, False, False, False]
['col1', 'col2', False, False, False, False]
['col1', 'col2', 'col3a', False, False, False]
['col1', 'col2', 'col3b', False, False, False]
['col1', 'col2', 'col3b', 'col4', False, False]
['col1', 'col2', 'col3b', 'col4', False, False]
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