1

I am trying to delete last 5 elements i.e. '%20' from list arr:-

arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']
length = 30
print(length)
print(len(arr))
print(arr)
for x in range(length, len(arr)):
    print(arr[x])
for x in range(length, len(arr)):
    del arr[x]
print(arr)

But I am getting following output. I am not getting why print(arr[x]) works but del arr[x] doesn't:-

30
35
['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']
%20
%20
%20
%20
%20
Traceback (most recent call last):
   File "test.py", line 9, in <module>
     del arr[x]
IndexError: list assignment index out of range

5 Answers 5

2

The index goes out of range because if you delete items from original array, the size of original array gets reduced.

Try pop() to delete items from last

arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']
length = 30
print(length)
print(len(arr))
print(arr)
for x in range(length, len(arr)):
    print(arr[x])
for x in range(length, len(arr)):
    arr.pop()
print(arr)
Sign up to request clarification or add additional context in comments.

Comments

1

When you delete each item the list gets shorter so your index runs off the end. You can use slice notation del arr[length:] which will be much faster than pop.

Comments

0

this is because u are iterating over a list that changes in each iteration.

>>> arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']
>>> for x in range(length, y):
    del arr[30]


>>> arr
['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a']

pythonic way :

>>> arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']

>>> del arr[30: ]
>>> arr
['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a']

Comments

0

You can use del arr[x:] for solving this error.

arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']

length = 30
print(length)
print(len(arr))
print(arr)
for x in range(length, len(arr)):
    print(arr[x])

for x in range(length, len(arr)):
    del arr[x:]

print(arr)

Comments

0

The other answers explain why you you got the error that you did. This answer explains a much simpler way to do the same thing.

If you know you want to remove the last 5 elements, just make a slice that omits the last five elements.

arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']
number_to_remove_from_end = 5
arr = arr[:-number_to_remove_from_end]
print(arr)

Output:

['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a']

Alternatively, if you really want to use the del keyword to ensure that the list ID remains the same then the following will work

arr = ['A', 't', 'u', 'l', '%20', 'K', 'r', 'i', 's', 'h', 'n', 'a', '%20', 'P', 'a', 't', 'n', 'a', '%20', 'B', 'i', 'h', 'a', 'r', '%20', 'I', 'n', 'd', 'i', 'a', '%20', '%20', '%20', '%20', '%20']
number_to_remove_from_end = 5
del arr[-number_to_remove_from_end:]
print(arr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.