adding elements from previous list to current list in Python with enumerate

Viewed 58

I have a list of lists as follows:

list1 = ['amazon', 'jeff bezos', '56']
list2 = ['', 'mackenzie bezos', '50']
list3 = ['', 'preston bezos', '20']
list4 = ['microsoft', 'bill gates', '64']
list5 = ['', 'melinda gates', '55']

where

lists = [list1, list2, list3, list4, list5]

Basically, I want the empty fields to be populated by the element in the same index from the previous list. How do I get my output to be as follows?

list1 = ['amazon', 'jeff bezos', '56']
list2 = ['amazon', 'mackenzie bezos', '50']
list3 = ['amazon', 'preston bezos', '20']
list4 = ['microsoft', 'bill gates', '64']
list5 = ['microsoft', 'melinda gates', '55']
4 Answers

This will work if any of the values are blank. Not just the first value.

list1 = ['amazon', 'jeff bezos', '56']
list2 = ['', 'mackenzie bezos', '50']
list3 = ['', 'preston bezos', '20']
list4 = ['microsoft', 'bill gates', '64']
list5 = ['', 'melinda gates', '55']

lists = [list1, list2, list3, list4, list5]

for i, arr in enumerate(lists[1:]): # Just iterate through lists but from the the 2nd list. Not the first.
    for j in range(len(arr)): # Iterate through inner list
        if not arr[j]: # Means blank or None
            arr[j] = lists[i][j]  # Replace value with the previous list. Remember i points one less because we have started from 2nd list
for arr in lists:
    print(arr)

#Output
['amazon', 'jeff bezos', '56']
['amazon', 'mackenzie bezos', '50']
['amazon', 'preston bezos', '20']
['microsoft', 'bill gates', '64']
['microsoft', 'melinda gates', '55']

Something like this should work:

lists = [list1, list2, list3, list4, list5]
for i, l in enumerate(lists):
    if not l[0]:
        l[0] = lists[i-1][0]

You could use a dictionary to keep track of the previous columns that have been seen, using the index as the key. Then update empty '' values with the previous found value at that column. I'm assuming the previous value can be anything but ''.

from pprint import pprint

list1 = ['amazon', 'jeff bezos', '56']
list2 = ['', 'mackenzie bezos', '50']
list3 = ['', 'preston bezos', '20']
list4 = ['microsoft', 'bill gates', '64']
list5 = ['', 'melinda gates', '55']

lists = [list1, list2, list3, list4, list5]

prev_col_values = {}
for row, lst in enumerate(lists):
    for col, word in enumerate(lst):
        if not word:
            lists[row][col] = prev_col_values.get(col, '')
            continue    
        prev_col_values[col] = word

pprint(lists)

Output:

[['amazon', 'jeff bezos', '56'],
 ['amazon', 'mackenzie bezos', '50'],
 ['amazon', 'preston bezos', '20'],
 ['microsoft', 'bill gates', '64'],
 ['microsoft', 'melinda gates', '55']]
def work(lists):
print("Before converting>>>\r\n")
print (lists)

tag_to_add = ''
for item in lists:
    if len(item[0]) > 0:
        tag_to_add = item[0]
    else:
        if len(tag_to_add) > 0:
            item[0] = tag_to_add

print ("After converting>>>\r\n")
print (lists)

Above is the code.

list1 = ['amazon', 'jeff bezos', '56']
list2 = ['', 'mackenzie bezos', '50']
list3 = ['', 'preston bezos', '20']
list4 = ['microsoft', 'bill gates', '64']
list5 = ['', 'melinda gates', '55']

lists1 = [list1,list2,list3,list4,list5]
work(lists1)

above is test case 1

list1 = ['amazon', 'jeff bezos', '56']
list2 = ['', 'mackenzie bezos', '50']
list3 = ['', 'preston bezos', '20']
list4 = ['microsoft', 'bill gates', '64']
list5 = ['', 'melinda gates', '55']

lists1 = [list2,list1,list3,list4,list5]
work(lists1)

above is test case 2

Related