1

I'm writing code to organize some textual data. In my code I need to check for a string and delete all list elements with that string in them. The following is part of my code:

for i in range(0, len(split_data)): 
    if 'Vienna' in split_data[i]: #list index out of range
        del split_data[i]

I understand that the error (List index out of range) means that I am calling a value using an index out of range of the list. However, I'm a bit confused, since I have used len(split_data) which would obviously be the length of the list. I would appreciate if anyone could give me some guidance or suggestions. [I am a novice programmer]

Here is a subset of the data in split_data: split_data ~ ['538980', '33', "OFFICER'S CHOICE", '1402', 'BDA LIMITED', '12, EVERGREE INUDSTRIAL ESTATE, SHANKTI MILLS LANE, MAHALAXMI, MUMBAI - 400 011.', 'Opposed', ' ', 'APPLICATION DATE : 29/10/1990', 'USER DATE :', 'GOODS/SERVICES : ALCOHOLIC BEVERAGES INCLUDING WHISKY, BRANDY, GIN, RUM, VODKA AND WINE INCLUDED IN CLASS 33.']

  • And you are sure your split_data list is not empty – abhinav kumar May 15 '17 at 05:40
  • can you show use whats inside split_data? – Hackaholic May 15 '17 at 05:40
  • Deleting an element inside the loop will change the length of the actual list. Try to loop over a copy of the list. – kuro May 15 '17 at 05:40
  • never loop over a list and delete element at same time. – Hackaholic May 15 '17 at 05:41
  • Do not delete the items in a list while traversing over it. Possible duplicate of http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating – DineshKumar May 15 '17 at 05:41
  • 1
    Possible duplicate of [Remove items from a list while iterating](http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating) – DineshKumar May 15 '17 at 05:42
  • split_data ~ ['538980', '33', "OFFICER'S CHOICE", '1402', 'BDA LIMITED', '12, EVERGREE INUDSTRIAL ESTATE, SHANKTI MILLS LANE, MAHALAXMI, MUMBAI - 400 011.', 'Opposed', ' ', 'APPLICATION DATE : 29/10/1990', 'USER DATE :', 'GOODS/SERVICES : ALCOHOLIC BEVERAGES INCLUDING WHISKY, BRANDY, GIN, RUM, VODKA AND WINE INCLUDED IN CLASS 33.'] I think the error may be because I've changed the length of the list like kuro said. I'm trying to loop over a copy. – Aneesh Upadhyaya May 15 '17 at 05:50
  • @Aneesh, Please add this in your question, not in comment – kuro May 15 '17 at 05:54

2 Answers2

0

Its a bad idea to loop through the list and delete the elements

You can use list comprehension to create the new list

split_data = [x for x in split_data if 'Vienna' not in x]

or use filter

split_data = list(filter(lambda x : 'Vienna' not in x, split_data))
Praveen
  • 8,945
  • 4
  • 31
  • 49
0

The issue is with the range of values that you have given; here you want to check every string in the array, however you are making an unnecessary extra iteration at the end of the loop which is causing your program to give you an error.

Python references positions in an array starting at 0, and given that you only want to compare n number of strings, by writing

for i in range(0, len(split_data)):

you are making n+1 comparisons which is impossible. So, just reduce the number of comparisons that you make in the for loop like this:

for i in range(0, len(split_data)-1):

Hope that helps!

Daynarc
  • 61
  • 4