0

I am trying to write some code which adds an element in one list to another list and then removes it from the first list. It also should not add duplicates to the new list which is where the if statement comes in. However, when adding to the 'individuals' list and removing from the 'sentence_list' list it misses out certain words such as 'not' and 'for'. This is also not random and the same words are missed each time. Any help?

sentence = "I am a yellow fish"
sentence_list = sentence.lower().split()
individuals = []

for i in sentence_list:
  if i in individuals:
    print ("yes")
    sentence_list.remove(i)
  else:
    individuals.append(i)
    sentence_list.remove(i)
    print ("individuals", individuals)
    print ("sentence_list", sentence_list)
  • 1
    Would be helpful if you specify the language this is written in and add the language as a tag to your question. – almcd Mar 31 '17 at 10:48
  • Possible duplicate of [Remove items from a list while iterating](http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating) – SiHa Mar 31 '17 at 11:50
  • 1
    Please don't vandalize your post! – FelixSFD Apr 24 '17 at 10:47

2 Answers2

2

The issue is that you are removing items from the list you are looping through. You can fix this just by making a copy of the list and looping through it instead, like this:

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
sentence_list = sentence.lower().split()
individuals = []

#We slice with [:] to make a copy of the list
orig_list = sentence_list[:]
for i in orig_list:
  if i in individuals:
    print ("yes")
    sentence_list.remove(i)
  else:
    individuals.append(i)
    sentence_list.remove(i)
    print ("individuals", individuals)
    print ("sentence_list", sentence_list)

The lists are now what was expected:

print(individuals)
print(sentence_list)

['ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you']
[]
Kewl
  • 3,327
  • 5
  • 26
  • 45
1

In general you should not add or remove elements to a list as you iterate over it. Given that you are removing every single element of the list, just remove the lines with sentence_list.remove(i).

If you actually need to remove just some elements from the list you're iterating I'd either: make a new empty list and add the elements you want to keep to that, or keep a track of which indices in the list you want to remove as you iterate and then remove after the loop.

For the first solution,

oldList = [1, 2, 3, 4]
newList = []

for i in oldList:
    shouldRemove = i % 2
    if not shouldRemove:
        newList.append(i)

For the second,

oldList = [1, 2, 3, 4]
indicesToKeep = []

for i, e in enumerate(oldList):
    shouldRemove = e % 2
    if not shouldRemove:
        indicesToKeep.append(i)

newList = [e for i, e in enumerate(oldList) if i in indicesToKeep]
James Elderfield
  • 2,389
  • 1
  • 34
  • 39