-3
list1 = [1,2,3,4,5]
list2 = list1.insert(-1, 100)
list2 = [1,2,3,4,100,5]

I can't understand why the result becomes like above...

vvvvv
  • 25,404
  • 19
  • 49
  • 81
thsamajisama
  • 51
  • 1
  • 6

1 Answers1

2

the minus index counts from the right. So you're insert is inserting before the last item on the list. To add an item to the end of the list use:

list1.append(100)