# Apend and extend concept

hello guys… so I’m having this problem! why is it that the insert method reads from 5 to 0 and append reads from 0 to 5. I’m failing to get the concept behind. PLEASE HELP.

myList = # creating an empty list

for i in range(5):
myList.insert(0, i + 1)

print(myList)`

[timukudze]
“why is it that the insert method reads from 5 to 0 and append reads
from 0 to 5”

You are calling `myList.insert(0, i + 1)` so each number is inserted
before position 0, which is the beginning of the list. Always the

``````[] insert at position 0, 0+1 = 1, gives [1]
``````

Second time through the loop:

``````[1] insert at position 0, 1+1 = 2, gives [2, 1]
``````

And so forth:

``````[2, 1] insert at position 0, 2+1 = 3, gives [3, 2, 1]
[3, 2, 1] insert at position 0, 3+1 = 4, gives [4, 3, 2, 1]
[4, 3, 2, 1] insert at position 0, 4+1 = 5, gives [5, 4, 3, 2, 1]
``````

But appending always goes to the end, so you get:

``````[] --> [1]
[1] --> [1, 2]
[1, 2] --> [1, 2, 3]
[1, 2, 3] --> [1, 2, 3, 4]
[1, 2, 3, 4] --> [1, 2, 3, 4, 5]
``````

An easy way to see this is to print the list each time:

``````myList = [] # creating an empty list
for i in range(5):
print(myList)
myList.insert(0, i + 1)

print(myList)
``````
1 Like

Woo ow I’m so thankful now it makes sense hey. thank you very much

Wonderful answer, could go straight into a book.

I add this tip for ease of visualisation:

1. enter the code in: http://www.codeskulptor.org/viz/

2. Press the “wrench” button on the toolbar for “Viz mode”

3. Play and step through the statements using the arrow buttons

Check out the attachment to see how it was able to step back and
visualise intermediate state.

Marco Ippolito

maroloccio@gmail.com