Hi. I am confused with the below code. Would greatly appreciate an explanation on how it resulted to the correct answer:
my_list = [1, 2, 3]
for v in range (len(my_list)):
my_list.insert (1, my_list[v])
print (my_list)
[1, 1, 1, 1, 2, 3]
I understood it as → for each value in range (3) which is 0,1,2 → we will insert 0,1,2 to the index 1 position in the list. So for each time, I came up with the ff. new lists:
I inserted 0 to the index 1 position – > [1, 0, 2, 3]
Then I inserted 1 to the index 1 position → [1, 1, 0, 2, 3]
Then I inserted 2 to the index 1 position → [1, 2, 1, 0, 2, 3]
So my answer was [1, 2, 1, 0, 2, 3] which is wrong. Why?
Thanks!