Python: Concatenating sublists between different lists -
i have 2 lists of lists, of equal length, this:
lsta = [[1,4,5,6],[4,5],[5,6],[],[],[],[],[]] lstb = [[7,8],[4,5],[],[],[],[2,7,8],[7,8],[6,7]] and want concatenate sublists @ each index position such make single sublist, this:
newlst = [[1,4,5,6,7,8],[4,5],[5,6],[],[],[2,7,8],[7,8],[6,7]] ideally, new sublists remove duplicates (like in newlst[1]). converted integers strings, , attempted this:
for in range(len(lsta)): c = [item + item item in stra[i], strb[i]] but adds each item each list before adding other list, resulting in this:
failedlst = [[["1","4","5","6","1","4","5","6"],["7","8","7","8"]],[["4","5","4","5"],["4","5","4","5"]]...etc] and still doesn't join 2 sublists, makes new sublist of 2 sublists. appeciated!
making list concatenating items in parallel simple, using list comprehension in combination zip function.
newlst = [x+y x,y in zip(lsta, lstb)] if want remove duplicates, can use set. if want put items in order in list, can use sorted.
in combination, this:
newlst = [sorted(set(x+y)) x,y in zip(lsta, lstb)]
Comments
Post a Comment