[Tutor] insert into list index -1

hemanth sai kumar nvhemanthsaikumar at gmail.com
Mon Sep 28 12:47:03 EDT 2020


Hi All,

Thank you all for helping the python community.

Regarding the insert into list index 0 and -1.

insert into 0 is reversing the list.

while inserting into -1 is showing as below. value 1 is fixed and further
insert into -1 index is shifting -2 index value

lst=[1,2,3,4,5,6]
t=[]
for i in lst:
    t.insert(-1,i)
    print("i value {} ".format(i))
    print(t)
print(lst)
print(t)


i value 1
[1]
i value 2
[2, 1]
i value 3
[2, 3, 1]
i value 4
[2, 3, 4, 1]
i value 5
[2, 3, 4, 5, 1]
i value 6
[2, 3, 4, 5, 6, 1]
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 1]


More information about the Tutor mailing list