1

I have a list like the following: [[10,10], [20,30], [30,50], [40, 60]] I want to add elements between every one of theme in order to obtain a list like:

[[10,10], [15, 20], [20,30], [25, 40], [30,50], [35, 55], [40, 60]]

My code is

`for i,j in enumerate(sub_list):
      list1.insert(i,j)`

where sub_list is [[15, 20], [25, 40], [35, 55]].

Output is : [[10, 10], [15, 20], [25, 40], [35, 55], [20, 30], [30, 50], [40, 60]]

Problem is that at the first insertion everything is shifted of course. What should I do ?

MM1
  • 912
  • 1
  • 10
  • 28
  • 1
    Your inserting all of the items after the first item and before the others. That's why it's shifting - your using the wrong index. – Legorooj Dec 05 '19 at 22:40
  • Your current approach doesn't work because the indexes don't take into account the modified `list1` after each iteration. I suggest building a new list from the two lists instead of inserting into one of the existing lists. The link at the top should provide a clue how to do that. You will need to make some changes since it assumes the input lists are the same length. – Code-Apprentice Dec 05 '19 at 22:53
  • Or you could iterate backwards inserting your new value into the last index in your list, then the second to last index and on to the first index you want your new value inserted – Ethan Dec 05 '19 at 23:28

0 Answers0