Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:06:26+00:00 2026-06-06T11:06:26+00:00

I’ve noticed that many operations on lists that modify the list’s contents will return

  • 0

I’ve noticed that many operations on lists that modify the list’s contents will return None, rather than returning the list itself. Examples:

>>> mylist = ['a', 'b', 'c']
>>> empty = mylist.clear()
>>> restored = mylist.extend(range(3))
>>> backwards = mylist.reverse()
>>> with_four = mylist.append(4)
>>> in_order = mylist.sort()
>>> without_one = mylist.remove(1)
>>> mylist
[0, 2, 4]
>>> [empty, restored, backwards, with_four, in_order, without_one]
[None, None, None, None, None, None]

What is the thought process behind this decision?

To me, it seems hampering, since it prevents "chaining" of list processing (e.g. mylist.reverse().append('a string')[:someLimit]). I imagine it might be that "The Powers That Be" decided that list comprehension is a better paradigm (a valid opinion), and so didn’t want to encourage other methods – but it seems perverse to prevent an intuitive method, even if better alternatives exist.


This question is specifically about Python’s design decision to return None from mutating list methods like .append. However, novices often write incorrect code that expects .append (in particular) to return the same list that was just modified. Please do close such questions as a duplicate of this one, however. "The code did the wrong thing because the result was None rather than the list" is something that the OP in these cases should have discovered independently via debugging; creating a proper MRE leaves behind a question like this one – therefore, it can be considered a duplicate.

See How can I collect the results of a repeated calculation in a list, dictionary etc. (or make a copy of a list with each element modified)? for the simple question of "how do I append to a list repeatedly?" (or debugging questions that boil down to that problem). This is a new canonical that has been specifically prepared to address the topic with the perspective that beginners lack.

To get modified versions of the list, see:

  • How to allow list append() method to return the new list
  • How can I get a sorted copy of a list?
  • How do I concatenate two lists in Python? (to replace .extend)
  • A quick way to return list without a specific element in Python (to replace .remove)
  • How can I get a reversed copy of a list (avoid a separate statement when chaining a method after .reverse)?

The same issue applies to some methods of other built-in data types, e.g. set.discard (see How to remove specific element from sets inside a list using list comprehension) and dict.update (see Why doesn't a python dict.update() return the object?).

The same reasoning applies to designing your own APIs. See Is making in-place operations return the object a bad idea?.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:06:28+00:00Added an answer on June 6, 2026 at 11:06 am

    The general design principle in Python is for functions that mutate an object in-place to return None. I’m not sure it would have been the design choice I’d have chosen, but it’s basically to emphasise that a new object is not returned.

    Guido van Rossum (our Python BDFL) states the design choice on the Python-Dev mailing list:

    I’d like to explain once more why I’m so adamant that sort() shouldn’t
    return ‘self’.

    This comes from a coding style (popular in various other languages, I
    believe especially Lisp revels in it) where a series of side effects
    on a single object can be chained like this:

    x.compress().chop(y).sort(z)
    

    which would be the same as

    x.compress()
    x.chop(y)
    x.sort(z)
    

    I find the chaining form a threat to readability; it requires that the
    reader must be intimately familiar with each of the methods. The
    second form makes it clear that each of these calls acts on the same
    object, and so even if you don’t know the class and its methods very
    well, you can understand that the second and third call are applied to
    x (and that all calls are made for their side-effects), and not to
    something else.

    I’d like to reserve chaining for operations that return new values,
    like string processing operations:

    y = x.rstrip("\n").split(":").lower()
    

    There are a few standard library modules that encourage chaining of
    side-effect calls (pstat comes to mind). There shouldn’t be any new
    ones; pstat slipped through my filter when it was weak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.