Search code examples
pythonstop-words

del doesnt seem to delete anything from a list


for x,y in words:
    for z in x:
        if z in stopwords:
            del x[x.index(z)]

This is my code. The data in words is a list of Tuples where a tuple looks like this:

(list of words, metadata)

The purpose of my code is to remove all the stopwords from the list of words. The only problem with this is, that the stopwords are not removed afterwards...

What exactly did I do wrong? I already tried to do it with

x.pop(x.index(z))

but that doesn't seem to make a difference.


Solution

  • You could simply create a new list without the stop words using a nested list comprehension:

    stopwords = set(stopwords)  # just so "in" checks are faster
    result = [([word for word in x if word not in stopwords], y) for x, y in words]
    

    For example:

    >>> stopwords = ['stop']
    >>> words = [(['hello', 'you', 'stop'], 'somemeta')]
    >>> stopwords = set(stopwords)  # just so "in" checks are faster
    >>> result = [([word for word in x if word not in stopwords], y) for x, y in words]
    >>> result
    [(['hello', 'you'], 'somemeta')]
    

    Note that you generally shouldn't modify the list you're iterating over. That could lead to a lot of hard to track down bugs.