1

I'm trying to use the following query to reverse elements in a list using for loop and without built-in function but for some reason last 2 elements are not getting added. Any idea what I'm doing wrong here?

l = [5,7,9,1,8]
output = []
for i,v in enumerate(l):
  output.append(l[len(l)-1])
  l.remove(l[len(l)-1])    
output

result: [8,1,9] expected: [8,1,9,7,5]

mavles
  • 103
  • 1
  • 7
  • 1
    And to just add to the duplicate, if you do `for i,v in enumerate(l[:]):`, which would use a copy of `l` to enumerate, or if you used `for i in range(len(l))`, you would not see this issue. – Bill Lynch Jan 09 '23 at 02:41

0 Answers0