Sometimes we need to sort the list and need to create a new copy of the list. We can sort the list using the sort() method and copy a list using the copy() method.
numbers = [23, 12, 54, 3, 2, 6, 11, 9, 7, 8]
numbers.sort()
print(numbers)
Output:
[2, 3, 6, 7, 8, 9, 11, 12, 23, 54]
numbers = [23, 12, 54, 3, 2, 6, 11, 9, 7, 8]
numbers.sort(reverse=True)
print(numbers)
Output:
[54, 23, 12, 11, 9, 8, 7, 6, 3, 2]
Sort in Descending Order:
numbers = [23, 12, 54, 3, 2, 6, 11, 9, 7, 8]
sorted_numbers = sorted(numbers)
print(numbers)
print(sorted_numbers)
Output:
[23, 12, 54, 3, 2, 6, 11, 9, 7, 8]
[2, 3, 6, 7, 8, 9, 11, 12, 23, 54]
numbers = [23, 12, 54, 3, 2, 6, 11, 9, 7, 8]
sorted_numbers = sorted(numbers, reverse=True)
print(numbers)
print(sorted_numbers)
Output:
[23, 12, 54, 3, 2, 6, 11, 9, 7, 8]
[54, 23, 12, 11, 9, 8, 7, 6, 3, 2]
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
new_list = numbers.copy()
Copy Using list() constructor:
numbers = ['one', 'two', 'three', 'four', 'fine', 'six']
new_list = list(numbers)