1# The sort() function will modify the list it is called on.
2# The sorted() function will create a new list
3# containing a sorted version of the list it is given.
4
5list = [4,8,2,1]
6list.sort()
7#--> list = [1,2,4,8] now
8
9list = [4,8,2,1]
10new_list = list.sorted()
11#--> list = [4,8,2,1], but new_list = [1,2,4,8]
12
1sorted(iterable, key=None, reverse=False)
2
3type(sorted(iterable, key=None, reverse=False)) = list
1>>> student_tuples = [
2... ('john', 'A', 15),
3... ('jane', 'B', 12),
4... ('dave', 'B', 10),
5... ]
6>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
7[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
8
1>>> x = [1 ,11, 2, 3]
2>>> y = sorted(x)
3>>> x
4[1, 11, 2, 3]
5>>> y
6[1, 2, 3, 11]
7
1nums = [4,8,5,2,1]
2#1 sorted() (Returns sorted list)
3sorted_nums = sorted(nums)
4print(sorted_nums)#[1,2,4,5,8]
5print(nums)#[4,8,5,2,1]
6
7#2 .sort() (Changes original list)
8nums.sort()
9print(nums)#[1,2,4,5,8]