Skip to content

Conversation

@NureddinSoltan
Copy link

@NureddinSoltan NureddinSoltan commented Jan 27, 2026

This PR clarifies that sorted() returns a new list without modifying the original.

Beginners often mistakenly assume that sorted(list) modifies the list in place.
The documentation now includes examples showing that:

  • the original list remains unchanged
  • the return value must be assigned to a variable

This change updates the Sorting Basics section of the sorting HOWTO


📚 Documentation preview 📚: https://cpython-previews--144272.org.readthedocs.build/

@python-cla-bot
Copy link

python-cla-bot bot commented Jan 27, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@NureddinSoltan
Copy link
Author

This is a small documentation-only clarification.
Could a maintainer please add the skip issue label?

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the sentence "returns a new sorted list" is already clear. In the beginning of the section we have:

There is also a :func:sorted built-in function that builds a new
sorted list from an iterable.

The proposed changes add more text and honestly make the section less digest. I'm however -0.5 with changing the example to the last example:

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

is transformed into

>>> integers = [5, 2, 3, 1, 4]
>>> sorted_integers = sorted(integers)
>>> sorted_integers
[1, 2, 3, 4, 5]

However, this would also make the example less digest and the problem is maybe rather with the implicit REPL output. So I would rather not change anything.

@bedevere-app
Copy link

bedevere-app bot commented Jan 27, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@NureddinSoltan
Copy link
Author

Thank you for reviewing! I remember when I first read this documentation, I was confused about what "returns a new sorted list" meant. Seeing the result in the shell made me think the original list was sorted.

I like your second suggestion of changing the first example to use variables and I agree with that. If you think that improvement is worth making, I'm happy to update the PR. Otherwise, I'm fine closing it.

@picnixz
Copy link
Member

picnixz commented Jan 27, 2026

Let's ask some documentation and teaching experts @terryjreedy @hugovk (I only taught Python to people already familiar with it so my opinion here is biased)

Copy link
Member

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NureddinSoltan, you did a claim:

Beginners often mistakenly assume that sorted(list) modifies the list in place.

Could you prove it somehow?

IMO, there is no issue and the PR must be closed.


A simple ascending sort is very easy: just call the :func:`sorted` function. It
returns a new sorted list:
returns a new sorted list and does not modify the original.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it's useless. HOWTO already very clear here and sorted docs also:

Return a new sorted list from the items in iterable.

Comment on lines +22 to +23
If you do not store or otherwise use the return value, the sorted result is
lost:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also redundant, given above sentence.

No need to explain every basic language feature in the HOWTO. We have the Tutorial for this.

@skirpichev skirpichev added the pending The issue will be closed if no feedback is provided label Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes docs Documentation in the Doc dir pending The issue will be closed if no feedback is provided skip issue skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants