0
lis =[-2,1,-3,4,-1,2,1,-5,4]
m=[]
for i in range(len(lis)):
        if len(lis)>=1:
                s = sum(lis)
                m.append(s)
                print(m)
                print(lis)
                lis.remove(lis[len(lis)-1])
                print(lis)

result : [1]
[-2, 1, -3, 4, -1, 2, 1, -5, 4]
[-2, 1, -3, -1, 2, 1, -5, 4]
[1, -3]
[-2, 1, -3, -1, 2, 1, -5, 4]
[-2, 1, -3, -1, 2, 1, -5]
[1, -3, -7]
[-2, 1, -3, -1, 2, 1, -5]
[-2, 1, -3, -1, 2, 1]
[1, -3, -7, -2]
[-2,1, -3, -1, 2, 1]
-----[-2, -3, -1, 2, 1]------this is the line that i didn't understand. why it remove the second index not the late index as i wrote in the code.
[1, -3, -7, -2, -3]
[-2, -3, -1, 2, 1]
[-2, -3, -1, 2]
[1, -3, -7, -2, -3, -4]
[-2, -3, -1, 2]
[-2, -3, -1]
[1, -3, -7, -2, -3, -4, -6]
[-2, -3, -1]
[-2, -3]
[1, -3, -7, -2, -3, -4, -6, -5]
[-2, -3]
[-2]
[1, -3, -7, -2, -3, -4, -6, -5, -2]
[-2]
[]

  • 1
    Use `lis.pop()` to remove (and return) the last element. – Jan Christoph Terasa May 28 '20 at 18:30
  • Aside from the cause being a common issue, I don't understand what *problem you are trying to solve* with this code. But also make sure you understand that the argument to the `.remove()` method of lists is **not** the index to remove at; it's the *value* to remove, and only the first instance of that value is removed. – Karl Knechtel May 28 '20 at 18:30
  • Remove does not use 'index' position. "The remove() method removes the first occurrence of the element with the specified value." – Anthony May 28 '20 at 18:39

0 Answers0