4 ms·
>same stuff within the standard library, like list.sort() and sorted(list) Those have different use cases. `sorted` is for situations where you would use a lis
by silly_giraffe 7y ago
>same stuff within the standard library, like list.sort() and sorted(list)
Those have different use cases. `sorted` is for situations where you would use a list comprehension and immediately consume the result vs `.sort` where you need the iterable sorted for future use.
As a quick scribble, you can do something like the following to sort random numbers in a single line:
result = sorted(random.random() for x in range(10))
From your Medium post:
def list_sort(arr):
return arr.sort()
def sorted_builtin(arr):
return sorted(arr)
Only the `sorted_builtin` function returns the array, `list_sort` returns None. The `list_sort` function would have to become:
def list_sort(arr):
arr.sort()
return arr
Edit: goofed the formatting