Example 1: how to store sorted list into new variable in python
>>> numbers = [6, 9, 3, 1]
>>> numbers_sorted = sorted(numbers)
>>> numbers_sorted
[1, 3, 6, 9]
>>> numbers
[6, 9, 3, 1]
Example 2: sorted vs sort python
list = [4,8,2,1]
list.sort()
list = [4,8,2,1]
new_list = list.sorted()
Example 3: python sort
>>> student_tuples = [
... ('john', 'A', 15),
... ('jane', 'B', 12),
... ('dave', 'B', 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Example 4: python sort
nums = [4,8,5,2,1]
sorted_nums = sorted(nums)
print(sorted_nums)
print(nums)
nums.sort()
print(nums)