How to efficiently merge two lists efficiently? Following is the code which gives incorrect results. Not sure. Why?
a = [[]] * 5
b = [[1], [] , [], [2], []]
for i in range(0, len(a)):
if b[i]:
a[i] += b[i]
Expected output:
[[1], [], [], [2], []]
Acutal output:
[[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
Result can be corrected by rewriting a[i] += b[i]
to a[i] = a[i] + b[i]
. However, I believe it would highly inefficient as it will result in creating a whole new list. FYI, each list will traversed a large numbers to need to be cache friendly if possible.