-2

I ran into a basic Python error.I want to know why is this error showing up.Can anyone please help me with this?

Code:

for i in range(0,len(l)):
    del l[i]
print(l)

Output:

Traceback (most recent call last):
  File "Hello.py", line 6, in 
    del l[i]
IndexError: list assignment index out of range
Tabaraei
  • 445
  • 2
  • 7
  • 18

1 Answers1

0

What are you trying to do? del removes element from the list. It doesn't to anything to the removed element. Are you trying to remove elements one by one from the list? What for? Anyway, after you remove an element, the list gets shorter, so eventually you get to an index that isn't there anymore.