2

I wanted to remove the word "hello" from this array, but I get the "index out of bounds" error. I checked the range of len(token); it was (0,5).

Here is the code:

token=['hi','hello','how','are','you']

stop='hello'

for i in range(len(token)):
    if(token[i]==stop):
        del(token[i])
peak
  • 105,803
  • 17
  • 152
  • 177
  • I tried to use this as a fixaround but got the same error Code " m=(len(tokens)); n=(len(stop)); for i in range(m): print(i) for j in range(n): print(j) if(tokens[i] == stop[j]): del(token[i]) m=m-1 " However adding it to a new list helped. Thanks – Chanakya Harish Feb 20 '16 at 00:44

6 Answers6

3

You're getting an index out of bounds exception because you are deleting an item from an array you're iterating over.

After you delete that item, len(token) is 4, but your for loop is iterating 5 times (5 being returned from the initial len(token)).

There are two ways to solve this. The better way would be to simply call

token.remove(stop)

This way won't require iterating over the list, and will automatically remove the first item in the array with the value of stop.

From the documentation:

list.remove(x): Remove the first item from the list whose value is x. It is an error if there is no such item.

Given this information, you may want to check if the list contains the target element first to avoid throwing a ValueError:

if stop in token:
    token.remove(stop)

If the element can exist multiple times in the list, utilizing a while loop will remove all instances of it:

while stop in token:
    token.remove(stop)

If you need to iterate over the array for some reason, the other way would be to add a break after del(token[i]), like this:

for i in range(len(token)):
    if(token[i]==stop):
        del(token[i])
        break
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
  • `while stop in token: token.remove(stop)` is a very inefficient approach to the problem. `in` has to iterate the list to search, and `.remove` repeats that iteration to figure out the index to delete, and then that entire process is iterated - looking at multiple non-removed elements multiple times each. Better approaches are shown in the linked duplicate. – Karl Knechtel Aug 03 '22 at 02:25
1

It's not recommended to delete a list element when iterating over this list. I'm not sure what you intend but you could create a new list without the stop

token=['hi','hello','how','are','you']
stop='hello'

new_tokens = []
for i in range(len(token)):
    if(token[i]!=stop):
        new_tokens.append(token[i])

or create a list with everything until stop is reached:

token=['hi','hello','how','are','you']
stop='hello'

new_tokens = []
for i in range(len(token)):
    if(token[i]!=stop):
        new_tokens.append(token[i])
    else:
        break

But never delete elements from a list you are iterating over because then the length of the list is modified but the range is not.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

The reason you are getting this error is two fold:

  • you are using the anti pattern in Python of range(len(sequence)). You should use for index, value in enumerate(sequence)
  • You are mutating a sequence as you iterate across it.

The call to range(len(...)) is only evaluated once. So when you star it evaluates to 5. Once you remove your stop word the list no longer has 5 elements so token[4] results in an IndexError

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
0

Once you delete an item, there are no longer as many items in the list as there were originally, so i will get too big. Also, if you delete the item at index i, then the element that used to be at i+1 will now be at index i, but your code won't check it, since it goes ahead and increments i.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Use break statement after deleting because you are modifying the same list in which you are iterating.

for i in range(len(token)):
  if(token[i]==stop):
    del(token[i])
    break 
Code2end
  • 116
  • 8
0

I don't disagree with any of the other answers, but the most Pythonic solution is to get rid of the loop entirely and replace it with one line:

token.remove(stop)

That will remove the first occurrence of 'hello' from the list.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24