NLTK
Python
stop words
text processing
natural language processing

How to remove stop words using nltk or python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Stop-word removal is a standard preprocessing step in natural language processing, but it is not automatically a good idea for every task. The mechanics are simple in Python, yet the real value comes from choosing the right tokenization, normalization, and stop-word list for your data.

What Stop Words Are Good For

Stop words are frequent words such as “the”, “is”, and “and” that often contribute less signal in bag-of-words style models. Removing them can shrink the vocabulary size and reduce noise for tasks like keyword extraction, topic modeling, or simple text classification.

That said, stop words are not always disposable. Words such as “not” and “no” can be important for sentiment or intent detection. The right approach depends on the task, not on habit.

Remove Stop Words with NLTK

NLTK includes a ready-made stop-word corpus for many languages. A typical workflow looks like this:

python
1import nltk
2from nltk.corpus import stopwords
3from nltk.tokenize import word_tokenize
4
5nltk.download("punkt")
6nltk.download("stopwords")
7
8text = "This is a simple example showing how to remove stop words from a sentence."
9
10tokens = word_tokenize(text.lower())
11stop_words = set(stopwords.words("english"))
12
13filtered = [token for token in tokens if token.isalpha() and token not in stop_words]
14
15print(filtered)

This code does four important things:

  • It tokenizes the text instead of splitting only on spaces.
  • It lowercases the words so The and the are treated the same.
  • It converts the stop words to a set for fast lookup.
  • It filters out punctuation by keeping only alphabetic tokens.

For many small projects, that is enough to get started.

Remove Stop Words with Plain Python

If you do not want the NLTK dependency, you can use a custom Python set and simple tokenization logic:

python
1import re
2
3STOP_WORDS = {
4    "a", "an", "the", "and", "is", "in", "of", "to", "for", "on"
5}
6
7
8def remove_stop_words(text: str) -> list[str]:
9    tokens = re.findall(r"[A-Za-z']+", text.lower())
10    return [token for token in tokens if token not in STOP_WORDS]
11
12
13text = "This is an example of removing stop words with plain Python."
14print(remove_stop_words(text))

This is lightweight and fully controllable. It is often a better fit for production code when the stop-word list is domain-specific and you do not need a full NLP toolkit.

Customize the Stop-Word List

Off-the-shelf stop-word lists are starting points, not ground truth. Domain language changes what counts as “uninformative.” In legal, medical, or product-search text, words that look common may still be important features.

You can add or remove words from the NLTK list easily:

python
1custom_stop_words = set(stopwords.words("english"))
2custom_stop_words.discard("not")
3custom_stop_words.update({"please", "thanks"})
4
5filtered = [
6    token for token in word_tokenize("Please do not remove important words")
7    if token.isalpha() and token.lower() not in custom_stop_words
8]
9
10print(filtered)

This is a practical way to preserve negation while still removing conversational filler.

Think About the Downstream Task

Stop-word removal helps some pipelines more than others. For example:

  • Bag-of-words classification often benefits from it.
  • Search indexing may benefit, depending on query behavior.
  • Sentiment analysis may suffer if you remove negations.
  • Transformer-based models often do not need manual stop-word removal at all.

That means “remove stop words” should be treated as an experiment, not a default law. Compare model quality with and without the step before hardcoding it into preprocessing.

Common Pitfalls

The biggest pitfall is removing stop words before you understand the task. In sentiment work, dropping “not” can completely change the meaning of a sentence.

Another common mistake is checking stop words before normalizing case. If the text is not lowercased consistently, many stop words will slip through.

It is also easy to tokenize too naively. Splitting on spaces alone leaves punctuation attached to tokens and makes stop-word matching less reliable.

Finally, avoid assuming the default NLTK stop-word list is correct for your domain. Generic lists are useful, but custom additions and removals are often what make the preprocessing actually effective.

Summary

  • NLTK provides a convenient stop-word corpus and tokenizer for standard NLP preprocessing.
  • Plain Python with a custom set is often enough when you want a lightweight solution.
  • Normalize text before comparing tokens against a stop-word list.
  • Customize the stop-word list to preserve meaningful words such as negations.
  • Measure the impact on the downstream task instead of assuming removal always helps.

Course illustration
Course illustration

All Rights Reserved.