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 8080087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:18:54+00:00 2026-06-05T16:18:54+00:00

In Python lists, there are two alternative ways of performing a list sort: Using

  • 0

In Python lists, there are two alternative ways of performing a list sort:

  1. Using its .sort method, which sorts in place
  2. Using the sorted() built-in function, which returns a new sorted list

Now, if I create a custom container type (say by subclassing from itertools‘s ABC), can I do the same?

I can figure out how to make the custom container object sorts in place. But what I want is for the built-in sort() to return a new instance of that custom container object (instead of a list) when I use it.

Is this possible? If so, how?

  • 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-05T16:18:55+00:00Added an answer on June 5, 2026 at 4:18 pm

    What about just subclassing the builtin list, and just redefining sort method ?

    >>> class MyList(list):
        def sort(self):
            copy = list(self)
            copy.sort()
            return MyList(copy)
    
    
    >>> l2 = MyList([2, 6, 9, 8])
    >>> l2
    [2, 6, 9, 8]
    >>> l3 = l2.sort()
    >>> l2
    [2, 6, 9, 8]
    >>> l3
    [2, 6, 8, 9]
    >>> 
    

    EDIT: after the OP’s comment, leaving sort method as is and adding a new one:

    >>> class MyList(list):
        def sort_copy(self):
            copy = list(self)
            copy.sort()
            return MyList(copy)
    

    The sort method inherited from parent remains as is (in place), the new sort_copy method creates a new one.

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

Sidebar

Related Questions

Is there any elegant way of splitting a list/dict into two lists/dicts in python,
I've seen there are actually two (maybe more) ways to concatenate lists in Python:
Is there a clever way to iterate over two lists in Python (without using
In Python, is there a good way to interleave two lists of the same
Is there any build-in function in Python that merges two lists into a dict?
In python, we can concatenate lists in two ways: lst.extend(another_lst) lst += another_lst I
In Python, the only way I can find to concatenate two lists is list.extend
Here’s my problem, I have two different lists, list a which contains the name
I have passed Python list to template like l=['text','there is no salary','I need alcohol','and
Is there a way in Python to list all the currently in-use drive letters

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.