Python - Sorting
Last Updated: 2123Z 11SEP19
(Created: 2123Z 11SEP19)
Signatures:
sorted(iterable, *, key=None, reverse=False)
Return a new sorted list from the items in iterable. (shallow copy)
list.sort(key=None, reverse=False)
random.shuffle(x)
Shuffle the sequence x in place
random.sample(population, k)
Return a k length list of unique elements chosen from the population sequence or set.
Examples:
# Simple
new_list = sorted(my_list) # new shallow copy
my_list.sort() # in place
# Lambda sort
my_list = [(0, 208), (1, 485), (2, 327), (3, 852), (4, 494)]
list_sorted = sorted(my_list, key=lambda obj: obj[1])
# Suffle lists
import random
new_list = random.sample(my_list, len(my_list)) # new shallow copy
random.shuffle(my_list) # in place
References: