Manipulating list values in python I wrote a function that I use to preprocess pandas dataframes before running them through a machine learning model. The function works perfectly, however I don't think it's the most pythonic way to write it.

This function accepts a list of words:

['here', 'is', 'a','sample', 'of','what','the','function','accepts']


def clean_text(x):

    stopwords_english = stopwords.words('english')

    for i,word in enumerate(x):
        if word.lower() in stopwords_english:
            x[i] = ''
        else:
            for punct in "/-'":
                x[i] = word.replace(punct, ' ')
            for punct in '&amp;':
                x[i] = word.replace(punct, f' {punct} ')
            for punct in '?!.,"#$%\'()*+-/:;&lt;=&gt;@[\\]^_`{|}~' + '“”’':
                x[i] = word.replace(punct, '')
    return x


Here I am using enumerate to change the value inside of the list. I would have assumed a more pythonic way of doing it would be writing it as follows:

def clean_text(x):

    stopwords_english = stopwords.words('english')

    for word in x:
        if word.lower() in stopwords_english:
            word = ''
        else:
            for punct in "/-'":
                word = word.replace(punct, ' ')
            for punct in '&amp;':
                word = word.replace(punct, f' {punct} ')
            for punct in '?!.,"#$%\'()*+-/:;&lt;=&gt;@[\\]^_`{|}~' + '“”’':
                word = word.replace(punct, '')
    return x


The function is being called as follows:

train['question_text'].progress_apply(lambda x: clean_text(x))


Where train is a pandas dataframe and 'question_text' is a column in the dataframe.

Is my current implementation the most pythonic way, or is there a better way?
This may be a good case for generator functions.  And splitting it into two parts might make things more flexible: first, remove the stopwords; and second, handle the punctuation.  

Also, str.maketrans and str.translate can do the punctuation mapping.

def remove_stopwords(text_iterable, stopwords):
    for word in text_iterable:
        if word.lower() not in stopwords:
            yield word


def handle_punctuation(text_iterable, table):
    for word in text_iterable:
        yield word.translate(table)


# format is ("chars", "replacement")          
mappings = (("/-'", ' '),
            ('&amp;', ' &amp; '),
            ('?!.,"#$%\'()*+-/:;&lt;=&gt;@[\\]^_`{|}~' + '“”’', None))

table = str.maketrans({c:v for k,v in mappings for c in k})

stopword_free = remove_stopwords(text, stopwords)
cleaned_text = handle_punctuation(stopword_free, table)


cleaned_text is a generator, use list(handle_punctuation(stopword_free, table)) if you need an actual list of words.