That is an interesting question, I would like to start by admitting that you are right, both solutions are correct and will produce the same results. But if we would work together and I would get the first version of the script for a code review I would definitely leave a note that you should change this code.
I will try to explain why the second version is better, but let me start by saying that the reasons might be not simple to grasp, because they are related to everything that happens when you are done with writing the application and many students have problems with understanding these reasons as they might be too abstract. So bear with me here
As we already established, both scripts are equal when it comes to the results. So now we have to ask ourselves some questions to establish which solution is better when it comes to the maintenance of this script. And this process is not trivial, you have to think about possible situations when something might break or when some trivial task becomes too complicated. Ok, few exemplary questions:
- what if I would like to add a next word that should be treated by the script as unnecessary?
- what if I would like to change the source of unnecessary words? maybe I would like to keep them in the external database so the users will have ability to add their own words?
- what if I would like to create a new function that would capitalize unnecessary words instead of deleting them?
Try to answer these questions and you will come to the conclusion that the second version of the script is much more easy to modify and maintain. For example, if we would like to change the source of words, from hard-coded strings to a list of words downloaded from the database or some sort of API in the first script you would have to change the logic (and the change would result in the code from the second version), because now you have a variable that holds the strings.