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:
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]