Why is my original list also sorted? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Why is my original list also sorted? (/thread-28363.html) |
Why is my original list also sorted? - Pedroski55 - Jul-15-2020 I have a simple list of words and a function to sort those words. def sortAlpha(words): words.sort() return words;I get the words: def gettheWords(): textFile = pathToText + 'words' textFileBody = open(textFile, 'r') words = textFileBody.readlines() textFileBody.close() return words; theWords = gettheWords()Then I do: sortedWords = sortAlpha(theWords)But when I look at my original list: theWords, that list is also sorted alphabetically. Why is that?? It seems Python does not make a new list, just uses the new name as an alias for the original? Is there some way to change this behaviour? RE: Why is my original list also sorted? - Yoriz - Jul-15-2020 words.sort() sorts the list in placesortedwords = sorted(words) will return a new sorted list
|