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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:39:10+00:00 2026-06-03T02:39:10+00:00

I am trying to create defaultlist , by analogy with defaultdict . The idea

  • 0

I am trying to create defaultlist, by analogy with defaultdict. The idea is that sometimes I want a list that is pre-filled (virtually!) with some element at all indices for which the value is not explicitly set. The difference with defaultdict is that I do not actually add the default values to the list, unless I have to extend list due to user assignments.

1) Is there any problem with this design?

2) This is the implementation I’m going to use. Is there any issue with it?

class defaultlist(list):
  def __init__(self, default_factory, arg = ()):
    # note: cannot use arg = None since list(None) is not the same as list()
    # alternatively can use:
    # def __init__(self, default_factory, *args, **kwargs)
    self.default_factory = default_factory
    super().__init__(arg)

  def __getitem__(self, key):
    try:
      return super().__getitem__(key)
    except IndexError:
      return self.default_factory()

  def __setitem__(self, key, value):
    for i in range(len(self), key):
      self.append(self.default_factory())
    self.append(value)

EDIT: Originally, I had two mistakes. Sorry, and thanks for pointing out both errors in the answers.

  • 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-03T02:39:11+00:00Added an answer on June 3, 2026 at 2:39 am

    Problem.

    1. Your __init__ function tries to forward all arguments to the base class. However, the way you are writing it can only forward 1 argument, and the constructor will always expect the 2nd argument. The proper way to do that in Python is:

      def __init__(self, default_factory, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.default_factory = default_factory
      
    2. The __setitem__ function tries to fill all not-yet-existing indices, but you have missed the key itself — note that range is exclusive in the end — therefore the super().__setitem__ method will fail. You should use .append in that case.

      def __setitem__(self, key, value):
          try:
              super().__setitem__(key, value)
          except IndexError:
              for i in range(len(self), key):
                  self.append(self.default_factory())
              self.append(value)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a list equivalent for the very useful collections.defaultdict . The
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
I am trying create a small web application that allows a user to login
Trying to create a simple document that gets the parentNode then applies a background
Trying to create a very piece of validation to prevent spammers. I want a
I'm trying create this function such that if any key besides any of the
I'm trying create a query that will output a total number, as well as
Trying to create an index (and run some long queries) on DB2 v9.1 and
I am trying create a WCF service that leverages the WPF MediaPlayer on the

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.