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

  • Home
  • SEARCH
  • 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 6567843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:23:05+00:00 2026-05-25T14:23:05+00:00

consider the following code if insert() return the list itself. def sieve(l): if not

  • 0

consider the following code if insert() return the list itself.

def sieve(l):
  if not len(l):
    return []
  return sieve(filter(lambda x: x%l[0] != 0, l)).insert(0, l[0])

For now, we have to rely on a helper function to return the list after insertion.

def cons(a, l):
  l.insert(0, a)
  return l

def sieve(l):
  if not len(l):
    return []
  return cons(l[0], sieve(filter(lambda x:x%l[0] != 0, l)))

The point of mutable/immutable object is totally valid.

However, for lists that are mutable, IMHO, append() API could take one more step to return the list itself, rather than not returning anything. Java StringBuilder is a good example. I can do chained append on stringbuilder object recursively….Just wish we could have this here also.

  • 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-05-25T14:23:06+00:00Added an answer on May 25, 2026 at 2:23 pm

    In Python, it is a big deal to get beginners to learn which objects are “immutable” and cannot be changed, and which are “mutable” and can be altered — in the latter case, every reference to the object sees the same change. This really seems to confuse newcomers. “I innocently called this function I wrote, and suddenly my copy of the list is changed too!”

    So Python has a convention: immutable objects, when you ask them to make an adjustment, return the newly-created object that is the answer — so:

    a = 'my string'
    b = a.replace('y', 'e')
    

    makes b get an entirely new string while a keeps its original value. Obviously such methods must return a value, since you cannot see the change, ever, by inspecting the original, immutable object itself.

    But when you ask a mutable object to change itself, it does not return itself, because it does not need to — you can see the change just by looking at the original object again! This is a critical semantic signal in Python: if a method like append() does not return a new object, then you can see the change just by looking at the old object, and so can everyone else with a reference to the old object.

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

Sidebar

Related Questions

Consider the following code: class Foo(var name: String = bar) Now i try to
Consider the following code: @try { if (something.notvalid) { return; } // do something
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider following code public class City { public string Name { get { return
I have a question regarding exception handling. Consider following Java code snippet. try{ //code
Consider the following code, I have a textblock that is an on/off button in
Consider the two lambda functions in the following VC++ 10.0 code: template <typename T>
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors

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.