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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:07:55+00:00 2026-05-13T06:07:55+00:00

I need a list with the following behavior >>> l = SparseList() >>> l

  • 0

I need a list with the following behavior

>>> l = SparseList()
>>> l
[]
>>> l[2] = "hello"
>>> l
[ None, None, "hello"]
>>> l[5]
None
>>> l[4] = 22
>>> l
[ None, None, "hello", None, 22]
>>> len(l)
5
>>> for i in l: print i
None
None
"hello"
None
22

Although it can “emulated” via a dictionary, it’s not exactly the same. numpy array can behave this way, but I don’t want to import the whole numpy for something like this. Before coding it myself, I ask if something similar exists in the standard library.

  • 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-13T06:07:55+00:00Added an answer on May 13, 2026 at 6:07 am

    Here’s minimal code to pass your given examples (with indispensable adjustments: you expect weird spacing and quoting, ‘None’ to be printed out at the prompt without a print statement, etc etc):

    class SparseList(list):
      def __setitem__(self, index, value):
        missing = index - len(self) + 1
        if missing > 0:
          self.extend([None] * missing)
        list.__setitem__(self, index, value)
      def __getitem__(self, index):
        try: return list.__getitem__(self, index)
        except IndexError: return None
    
    __test__ = dict(allem='''
    >>> l = SparseList()
    >>> l
    []
    >>> l[2] = "hello"
    >>> l
    [None, None, 'hello']
    >>> print l[5]
    None
    >>> l[4] = 22
    >>> l
    [None, None, 'hello', None, 22]
    >>> len(l)
    5
    >>> for i in l: print i
    None
    None
    hello
    None
    22
    ''')
    import doctest
    doctest.testmod(verbose=1)
    

    I imagine you’ll want more (to support negative indices, slicing, and whatever else), but this is all your examples are implicitly specifying.

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

Sidebar

Related Questions

I have the following List<int> collection and I need to find the highest integer
If we have the following list: list = ['UMM', 'Uma', ['Ulaster','Ulter']] If I need
I need to style a regular html list like the following picture: as you
I have the following code. I need to create list separators before A elements,
From the following HTML I need to figure out which List tag has class
i have the following query to list the employees of two table. i need
I need a list from OldGuids that are not in the NewGuids, so I
i need to sort the following list of Tuples in Python: ListOfTuples = [('10',
I need to JSON-encode a list using mochiweb. How do I get the following:
I need to parse a the following list format into a dict. This represents

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.