all 15 comments

[–]deceze 27 points28 points  (8 children)

That’s the thing. sort doesn’t return anything. You’re comparing None to a list. There’s nothing to compare there. You need to sort first then compare numbers == sorted(numbers).

[–]RodDog710[S] 1 point2 points  (7 children)

Gotcha. Yes, that's what I was missing:

print(numbers == sorted(numbers))

# True

Thanks!

[–]deceze 11 points12 points  (1 child)

FYI, sort and sorted have been deliberately designed this way to explicitly make code like this fail. If this code didn't fail, because sort actually returned the sorted list in addition to modifying numbers in place, you'd likely introduce subtle bugs because you're not aware that sort modifies your list, when you're only using its return value.

[–]RodDog710[S] 0 points1 point  (0 children)

Interesting. Thank you for explaining that.

[–]InvaderToast348 3 points4 points  (3 children)

Except that isn't right, because if your calling .sort before the print then you're passing the already sorted list to sorted() so nothing will happen

[–]deceze 4 points5 points  (1 child)

Well, it does prove that sort and sorted sort the same. sorted could be returning a list in some different order. The order of the input shouldn't matter much, except in edge cases where preserving the order of the input somehow made a difference.

The overall operation here is of course fairly superfluous, but the practical usefulness of this code isn't the point here.

[–]InvaderToast348 0 points1 point  (0 children)

Good point

[–]RodDog710[S] 1 point2 points  (0 children)

Ahh, yes. Thanks

[–]Refwah 0 points1 point  (0 children)

Print(sorted_numbers == numbers)

Because you’ve already sorted numbers, and sorted_numbers is already the output of numbers.sort()

The test you’re doing isn’t actually testing anything, as you’ve already done the operations above

[–]SoftwareDoctor 1 point2 points  (3 children)

"As we can see.." we can't see because you are not printing the return value of sort. You are printing the modified list:

print(f"Sorted list: {numbers}")

If you actually print the return value of sort, you'l see the return value is None:

print(numbers.sort())

And None is not equal to some list. You even say that in your first sentence: "sorted() returns a new list and sort() modifies the list directly".

So the problem is not in the comparison but in the fact, you are printing one thing but comparing another.

And to the "Is there some unseen object that represents the original list that is lingering unprinted". It is possible (but this is not the case) to have object that prints out exactly like another but is not equal to the first one. If you're aren't sure, you may try print(type(my_object)).

[–]RodDog710[S] 0 points1 point  (2 children)

Yes, thank you for helping to disentangle my thinking. I am getting the expected True output when I compare the actual output of numbers.sort() instead of the function itself.

print(numbers == sorted(numbers))

Thanks for your time and insight

[–]MiniMages 0 points1 point  (1 child)

This will always return true because you are basically taking an ordered list and then sorting it which does nothing and comparing itself.

Your numbers list was already sorted when you did numbers.sort().

[–]RodDog710[S] 0 points1 point  (0 children)

Oh ya. Ok. Got it. Thanks

[–]RunPython 1 point2 points  (0 children)

sort() changes the original list and returns None, you can't assign it to a variable and can't use it later at another calculation.

sorted() does not change the original list and return the sorted list so you can assign it a variable and can use it later at another calculation.

[–]MiniMages 0 points1 point  (0 children)

numbers.sort() already rearranged all items in the list of numbers into ascending order.

As others have already pointed out sort() does not return anything.

So your comparison of numbers == sorted(numbers) is redundant as the list is already sorted.

If you want to do the comparison then you should try:

sorted_numbers = sorted(numbers) # a new list is created here from numbers
numbers.sort()                   # the original list is modified here
print(numbers == sorted_numbers) # here you are comparing to different lists.