About .sort() method


#1

Then, we sort animals into alphabetical order. Note that .sort() modifies the list rather than returning a new list.
What exacttly this line means, please give brief discription.

link:
https://www.codecademy.com/courses/learn-python/lessons/python-lists-and-dictionaries/exercises/more-with-for?action=lesson_resume&link_content_target=interstitial_undefined


#2

well, we also have a sorted() function, this list returns a new list, so this preserves the unordered list:

start_list = [5, 3, 1, 2, 4]
sorted_list = sorted(start_list)
print sorted_list 
print start_list

so sorted() returns a ordered copy of the original list

on the other hand, the .sort() method modifies the original list, so we have 1 sorted list in the end, that is it, as we can prove with code:

start_list = [5, 3, 1, 2, 4]
start_list.sort()
print start_list

see? The original list gets modified


#3

I understood the difference.
thank you


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.