0

THis code is to remove words from a lists starting with a specified letter of the alphabet. The trouble is, is that it only removes some of the entries. when I run it to remove words beginning with 'a', only 'ana', 'agra', 'aba' are removed. Why? also is it possible to rewrite the condition to include a range of letters, say a-i for example?

def delete_from_list():
    discarded=[]
    #list_of_terms = pickle.load( open( "list_it.txt", "rb" ) )
    list_of_terms = ['ana', 'agro','agra',' aaa','aba','bab','Banana', 'band', 'bink' ]

    print('start length = ', len(list_of_terms))

    for item in list_of_terms:
        item.lower().strip()
        if item.startswith('a'):
            discarded.append(item)
            list_of_terms.remove(item)
    print('end_length = ', len(list_of_terms))
    print(discarded, list_of_terms)

Thank you for your time and help.

user1478335
  • 1,769
  • 5
  • 25
  • 37
  • Come on, it took me less than 30 seconds to find the duplicate – DeepSpace Nov 13 '17 at 13:28
  • When you remove something from a list while iterating through it, you're going to skip an item instead of going to the next one. Try printing the items inside the for loop, you don't actually examine all of them. Use `filter` or a list comprehension to accomplish tasks like this – Patrick Haugh Nov 13 '17 at 13:32
  • THank you for the help.I'll do that. I did not know this. The subtilties keep on eluding me, I'm afraid – user1478335 Nov 13 '17 at 13:33
  • Worked perfectly with filter. Thanks once again – user1478335 Nov 13 '17 at 13:55

1 Answers1

2

Like DeepSpace and Patrick said, you're iterating through the list while removing items from it. To look for words starting from a-i, you could try using regex.

regex=re.compile('^[a-i]')

if re.match(regex, somestring):
    # do things

The caret matches the start of the string, and the a-i inside the square brackets indicate the set of characters you want. See the python docs on regex for more detail.

Kevin
  • 352
  • 2
  • 4
  • 13
  • Thank you. I'll look at this carefully. I just wondered whether there was also someway of using startswith() with more than one value that I couldn't find. Obviously not. Appreciate you taking the time – user1478335 Nov 13 '17 at 13:40