atultherajput
atultherajput

Reputation: 175

Getting IndexError: list assignment index out of range in python

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

Upvotes: 1

Views: 1186

Answers (5)

SethMMorton
SethMMorton

Reputation: 48725

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)

Upvotes: 0

Md. Rezwanul Haque
Md. Rezwanul Haque

Reputation: 2950

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)

Upvotes: 0

Tomoki
Tomoki

Reputation: 176

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.

Upvotes: 1

pd shah
pd shah

Reputation: 1406

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']

Upvotes: 0

ksai
ksai

Reputation: 977

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)

Upvotes: 2

Related Questions