I have lists like:
L1 = [list]
L2 = [ [l1], [l2], ..., [ln] ]
I need to map these lists to
L = [ [list+l1] , [list + l2] , ..., [list + ln] ]
Right now I am just repeating L1 n times and then zipping them. Can someone please hint me to a more elegant way? In other words, I am looking to do this:
L=[]
L.append(L1 + L2[0])
L.append(L1 + L2[1])
...
It would also be nice if the solution would be robust to the following change:
L1 = [list1, list2, ..., listk]
and then L becomes
[ [list1 + list2 + ... + listk + l1] , ..., [list1 + list2 + ... + listk + ln] ]
Thanks!
is equivalent to
If
L1 = [list1, list2, ..., listk]is a list of lists, such asthen
list1 + list2 + ... + listkcan be formed withsum(L1, []):So in this case you could use
(Aside: It is also possible to use
L = [ sum(L1, [])+item for item in L2 ]but this would repeat the calculation ofsum(L1, [])once for each item inL2.)