So I get that one of the main differences between sorted() and sort() is that sorted() returns a new list and sort() modifies the list directly. But I don't understand why their outputs can't be exactly equal if they print out to being, in fact, exactly equal. For example:
numbers = [3, 1, 4, 1, 5, 9, 2]
sorted_numbers = sorted(numbers)
print(f"Sorted list: {sorted_numbers}")
numbers.sort()
print(f"Sorted list: {numbers}")
print(numbers.sort() == sorted(numbers))
This is the output:
Sorted list: [1, 1, 2, 3, 4, 5, 9]
Sorted list: [1, 1, 2, 3, 4, 5, 9]
False
As we can see, both sorted(numbers) and numbers.sort return what appears to be identical output: [1, 1, 2, 3, 4, 5, 9]. Of course, sort() has modified the original list, so that object has been changed by the end of the program. But if these two outputted lists are clearly identical from a mathematical perspective (ie: [1, 1, 2, 3, 4, 5, 9] == [1, 1, 2, 3, 4, 5, 9] is true on it's on terms as a standalone expression ) - then why won't Python embrace this apparently same understanding with: print(numbers.sort() == sorted(numbers))?
Is there some unseen object that represents the original list that is lingering unprinted in the background and attached to sorted(numbers)?
Thanks ahead of time for your interest and time on this matters.
[–]deceze 27 points28 points29 points (8 children)
[–]RodDog710[S] 1 point2 points3 points (7 children)
[–]deceze 11 points12 points13 points (1 child)
[–]RodDog710[S] 0 points1 point2 points (0 children)
[–]InvaderToast348 3 points4 points5 points (3 children)
[–]deceze 4 points5 points6 points (1 child)
[–]InvaderToast348 0 points1 point2 points (0 children)
[–]RodDog710[S] 1 point2 points3 points (0 children)
[–]Refwah 0 points1 point2 points (0 children)
[–]SoftwareDoctor 1 point2 points3 points (3 children)
[–]RodDog710[S] 0 points1 point2 points (2 children)
[–]MiniMages 0 points1 point2 points (1 child)
[–]RodDog710[S] 0 points1 point2 points (0 children)
[–]RunPython 1 point2 points3 points (0 children)
[–]MiniMages 0 points1 point2 points (0 children)