Python - merging two 2D lists, overwriting values of one -


i have two, 2 dimensional lists

list 1 = [[none, none], [none, none], [none, none]] list 2 = [[1, 2], [3, 4]] 

the desired output is

list 3 = [[1, 2], [3, 4], [none, none]] 

in case there simpler solution, i'm doing because don't have data third element in list. need list have 3 first level items. i'm trying stop script throwing out of range error.

some additional context address below questions.

this comes in app when database query returns result put list, e.g. 'biggest 10 problems'. things aren't bad, , there 9 problems.

when part of application looks 10th problem (a flask website), throws error. try/except indeed work, pretty heavy.

so need solution can create list of placeholder values, , overwrite them data if available.

the accepted solution works great 1 line of code.

i think meant

list_3 = [ list_2[i] if < len(list_2) else e i,e in enumerate(list_1) ] 

here equivalent without list comprehension

list_3 = list() i,e in enumerate(list_1):     if < len(list_2):         list_3.append(list_2[i])     else:         list_3.append(e) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -