2

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?

1 Answer 1

2

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]]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.