'Insert method output not as expected i thought it corresponds to (index,value) what is the reason behind this

why is the output as below

g=[] 
g.insert(3,5)
g.insert(2,6)
g.insert(1,7)
g.insert(0,8)

print(g) 

was expecting [8,7,6,5]

but the O/P is [8, 5, 7, 6]



Solution 1:[1]

    g= []
    g.insert(3,8)
    print(g[0]) -> O/P - 8
    print(g[3]) -> O/P - It will give an error.

If the list is empty and you will try to insert at any other position except 0, it will store that value only at position 0.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohan Patel