I'm trying to make a script that filters out a long list of words and only keeps the elements that have the length specified by the user. For some reason though, it would delete just above half the elements in the array. So I made a new program with less elements in the array to test it out and the same thing happens.
array = ['0', '1', '2a', '3b', '4aa', '5bb', '6aaa', '7bbb', '8aaaa', '9bbbb']
wordlength = input('Input length you do not want to delete')
for each in range(9):
try:
if len(array[each]) > int(wordlength) or len(array[each]) < int(wordlength):
array.pop(each)
print('The word,',array[each],', has been deleted')
except IndexError:
print(array)
If it matters, in my main program there are 113809 elements .