Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Doc: Clarify sorted() returns new list in sorting howto#144272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@NureddinSoltan
Copy link

@NureddinSoltanNureddinSoltan commentedJan 27, 2026
edited by github-actionsbot
Loading

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

Beginners often mistakenly assume thatsorted(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 theSorting Basics section of the sorting HOWTO


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

MSH1019, assim9, SEIFSEIF4, and ScamGames reacted with thumbs up emoji
@python-cla-bot
Copy link

python-cla-botbot commentedJan 27, 2026
edited
Loading

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?

assim9, SEIFSEIF4, and ScamGames reacted with thumbs up emojiassim9 and SEIFSEIF4 reacted with hooray emoji

Copy link
Member

@picnixzpicnixz left a comment
edited
Loading

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.

johnslavik, ZeroIntensity, and ScamGames reacted with thumbs up emoji
@bedevere-app
Copy link

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 phraseI 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.

MSH1019 reacted with thumbs up emoji

@picnixz
Copy link
Member

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)

NureddinSoltan reacted with thumbs up emoji

Copy link
Member

@skirpichevskirpichev 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.

@skirpichevskirpichev added the pendingThe issue will be closed if no feedback is provided labelJan 28, 2026
@picnixz
Copy link
Member

After a night of rest, I also think that my example would still be too complex for a HOWTO. I actually thought that the HOWTO was the tutorial but it seems that those are two different sections right? if this is the case I agree on closing the issue

NureddinSoltan reacted with thumbs up emoji

@NureddinSoltan
Copy link
Author

@skirpichev Thank you for asking for evidence. When I was searching before updating the PR, I found many beginners who were confused about this, likehere andhere.

To be honest, when I first read this documentation myself, I had to really think about it. Seeing the sorted result come out in the shell gave me the feeling that the list was sorted, even though I already knew it wasn't. That's why I thought making it more explicit with an example would help people who are going to read it for the first time.

But I understand if you think the current version is clear enough. Thanks for taking the time to review it!

@NureddinSoltan
Copy link
Author

Thank you@picnixz@StanFromIreland@skirpichev for taking the time to review this and for the discussion. I understand your perspective on keeping the documentation concise.

I'll close this PR now. I appreciate the feedback!

@NureddinSoltanNureddinSoltan deleted the improve-sorted-documentation branchJanuary 28, 2026 18:45
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@skirpichevskirpichevskirpichev requested changes

@picnixzpicnixzpicnixz requested changes

@StanFromIrelandStanFromIrelandAwaiting requested review from StanFromIreland

Assignees

No one assigned

Labels

awaiting changesdocsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is providedskip issueskip news

Projects

Status: Todo

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@NureddinSoltan@picnixz@skirpichev@StanFromIreland

[8]ページ先頭

©2009-2026 Movatter.jp