gython :
I have nested list like this:
ll =
[[[0, 0.01655718859584843],
[1, 0.03777621092166489],
[2, 0.02162311536578436],
[3, 0.02907007584458954]],
[[0, 0.011912058415296719],
[1, 0.07967490411502279],
[2, 0.04067120278932331],
[3, 0.05439173103552319]]]
I want to insert the entries the second list:
uu =
[4577911, 4577821]
into the index 0 of the corresponding sublist.
So into the first sublist of ll
I want to insert the first entry of uu
like this:
[[[4577911, 0, 0.01655718859584843],
[4577911, 1, 0.03777621092166489],
[4577911, 2, 0.02162311536578436],
[4577911, 3, 0.02907007584458954]],
[[4577821, 0, 0.011912058415296719],
[4577821, 1, 0.07967490411502279],
[4577821, 2, 0.04067120278932331],
[4577821, 3, 0.05439173103552319]]]
However my code delivers weird results
tu = ([[[u + x] for x in t] for t in ll for u in uu])
How can I do this right?
Rakesh :
Using nested list comprehension with zip
Ex:
ll = [[[0, 0.01655718859584843],
[1, 0.03777621092166489],
[2, 0.02162311536578436],
[3, 0.02907007584458954]],
[[0, 0.011912058415296719],
[1, 0.07967490411502279],
[2, 0.04067120278932331],
[3, 0.05439173103552319]]]
uu = [4577911, 4577821]
print([[[i] + k for k in j] for i, j in zip(uu, ll)])
Output:
[[[4577911, 0, 0.01655718859584843],
[4577911, 1, 0.03777621092166489],
[4577911, 2, 0.02162311536578436],
[4577911, 3, 0.02907007584458954]],
[[4577821, 0, 0.011912058415296719],
[4577821, 1, 0.07967490411502279],
[4577821, 2, 0.04067120278932331],
[4577821, 3, 0.05439173103552319]]]