IndexError: list index out of range in python for strings

Question:

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])
Asked By: Chanakya Harish

||

Answers:

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

Answered By: kylieCatt

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
Answered By: johnnyRose

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.

Answered By: MSeifert

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.

Answered By: Scott Hunter

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 
Answered By: Code2end

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.

Answered By: Paul Cornelius