About .sort() method


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.



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]
print start_list

see? The original list gets modified


I understood the difference.
thank you


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