# Insert Method in Python

**URL:** <https://discuss.python.org/t/insert-method-in-python/33565>\
**Category:** Python Help\
**Created:** [September 12, 2023, 3:24am UTC](https://discuss.python.org/t/insert-method-in-python/33565 "2023-09-12T03:24:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 12, 2023, 3:24am UTC](https://discuss.python.org/t/insert-method-in-python/33565/1 "2023-09-12T03:24:05Z")

</div>

Hi. I am confused with the below code. Would greatly appreciate an explanation on how it resulted to the correct answer:

```auto
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!

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [September 12, 2023, 4:00am UTC](https://discuss.python.org/t/insert-method-in-python/33565/2 "2023-09-12T04:00:21Z")

</div>

You seem to expect your code to insert `v` into `my_list` but you are inserting `my_list[v]` instead. The value of `my_list[v]` is not `v`, it’s whatever that element of the list is.

I suggest you print out the value you are inserting right before you do it. Heck, print out the list, too! This is a very simple way to see what your code is doing step-by-step…it wouldn’t be feasible with a really long loop but it’s fine here.

```auto
my_list = [1, 2, 3]
for v in range (len(my_list)):
    # print out the value of my_list, v, and my_list[v]
    print(f"{my_list=}\t{v=}\t{my_list[v]=}")
    my_list.insert (1, my_list[v])
print (my_list)

```

---

<div class="post-metadata">

**Author:** ![Moonlight001](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Moonlight001](https://discuss.python.org/u/Moonlight001)\
**Post date:** [September 12, 2023, 4:23am UTC](https://discuss.python.org/t/insert-method-in-python/33565/3 "2023-09-12T04:23:32Z")

</div>

Ohhh… I get it now! Thank you for the explanation code.
