'Modify list elements in python without using index

I am trying to perform stemming for a paragraph. The code I have tried is as follows.

sentences = nltk.sent_tokenize(paragraph)
stemmer = PorterStemmer()

for sentence in sentences:
    words = nltk.word_tokenize(sentence)
    words = [stemmer.stem(word) for word in words if word not in stopwords.words('english')]
    sentence = ' '.join(words)

The output I want is to replace the original sentences in the list sentences with the stemmed sentences, but I'm getting the same original sentences when printing the sentences

The correct output was obtained using the below code.

for i in range(len(sentences)):
    words = nltk.word_tokenize(sentences[i])
    words = [stemmer.stem(word) for word in words if word not in set(stopwords.words('english'))]
    sentences[i] = ' '.join(words) 

I just want to know what's wrong with my initial method.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source