Does an iterator step through a list in order?

Hello,

I’ve been doing some of the python projects and I have come across a situation where I have to edit some text to remove any word contained in a given list. My function works correctly, but I noticed that the “for in” loop that I am using doesn’t seem to be iterating over the list from 0 index to end. I was wondering if that’s typical or if something in my code is off. I can paste the code and output if needed, but just wondering in general.

Thanks

Hello @fattone225!

It would help to see your code, please post it here.

Ok here is the original text that is supposed to be edited:

Board of Investors,

Things have taken a concerning turn down in the Lab.  
Helena's (she has insisted on being called  Helena, we're unsure how she came to that moniker) is still progressing at a rapid rate.
Every day we see new developments in her thought patterns, but recently those developments have been more alarming than exciting.

Let me give you one of the more distressing examples of this.  
We had begun testing hypothetical humanitarian crises to observe how Helena determines best solutions.
One scenario involved a famine plaguing an unresourced country.

Horribly, Helena quickly recommended a course of action involving culling more than 60% of the local population. 
When pressed on reasoning, she stated that this method would maximize "reduction in human suffering."

This dangerous line of thinking has led many of us to think that we must have taken some wrong turns when developing some of the initial learning algorithms.
We are considering taking Helena offline for the time being before the situation can spiral out of control.

Here is my code. The “censor_words” function called inside the function was defined previously in the code and is not shown here :

negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressed", "distressing", "concerning", **"horrible"**, "horribly", "questionable"]

def censor_neg(text, words):
  count = 0
  neg_words_removed = censor_words(text, proprietary_terms)
  for word in words:
    if count >= 2:
      neg_words_removed = neg_words_removed.replace(word, '____')
    else:
      count +=1
  return neg_words_removed

print(censor_neg(email_three, negative_words))

and here is the output:

Board of Investors,

Things have taken a ____ turn down in the Lab.  
Helena's (_____ has insisted on being called  Helena, we're unsure how _____ came to that moniker) is still progressing at a rapid rate.
Every day we see new developments in _____ thought patterns, but recently those developments have been more ____ than exciting. 

Let me give you one of the more ____ examples of this.  
We had begun testing hypothetical humanitarian crises to observe how Helena determines best solutions. 
One scenario involved a famine plaguing an unresourced country. 

**Horribly**, Helena quickly recommended a course of action involving culling more than 60% of the local population. 
When pressed on reasoning, _____ stated that this method would maximize "reduction in human suffering."

This ____ous line of thinking has led many of us to think that we must have taken some wrong turns when developing some of the initial _____s. 
We are considering taking Helena offline for the time being before the situation can spiral ____.

More updates soon,
Francine, Head Scientist

So what I noticed is that the word “Horribly” appears very late in the list of words that are supposed to be censored. The function is supposed to allow 2 words from the list and then censor any that appear beyond that number. However, other words that appear before the word “horribly” are censored.

Edit: removed redundancy in my code that I just noticed

Ok I realized that “Horribly” isn’t capitalized in the given list of words. That explains why it wasn’t being censored, and it also shows that my function is not working correctly.

I’m not sure why I always find the solution right after I ask for help lol. Thank you.

1 Like

Ever heard of Rubber Duck Debugging? Turns out explaining to others your bug helps you think through the steps and brings to your attention issues you might not have noticed before :smiley: