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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:36:53+00:00 2026-06-04T10:36:53+00:00

How do I create an empty list that can hold 10 elements? After that,

  • 0

How do I create an empty list that can hold 10 elements?

After that, I want to assign values in that list. For example:

xs = list()
for i in range(0, 9):
   xs[i] = i

However, that gives IndexError: list assignment index out of range. Why?


Editor’s note:

In Python, lists do not have a set capacity, but it is not possible to assign to elements that aren’t already present. Answers here show code that creates a list with 10 "dummy" elements to replace later. However, most beginners encountering this problem really just want to build a list by adding elements to it. That should be done using the .append method, although there will often be problem-specific ways to create the list more directly. Please see Why does this iterative list-growing code give IndexError: list assignment index out of range? How can I repeatedly add elements to a list? for details.

  • 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-04T10:36:55+00:00Added an answer on June 4, 2026 at 10:36 am

    You cannot assign to a list like xs[i] = value, unless the list already is initialized with at least i+1 elements (because the first index is 0). Instead, use xs.append(value) to add elements to the end of the list. (Though you could use the assignment notation if you were using a dictionary instead of a list.)

    Creating an empty list:

    >>> xs = [None] * 10
    >>> xs
    [None, None, None, None, None, None, None, None, None, None]
    

    Assigning a value to an existing element of the above list:

    >>> xs[1] = 5
    >>> xs
    [None, 5, None, None, None, None, None, None, None, None]
    

    Keep in mind that something like xs[15] = 5 would still fail, as our list has only 10 elements.

    range(x) creates a list from [0, 1, 2, … x-1]

    # 2.X only. Use list(range(10)) in 3.X.
    >>> xs = range(10)
    >>> xs
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    Using a function to create a list:

    >>> def display():
    ...     xs = []
    ...     for i in range(9): # This is just to tell you how to create a list.
    ...         xs.append(i)
    ...     return xs
    ... 
    >>> print display()
    [0, 1, 2, 3, 4, 5, 6, 7, 8]
    

    List comprehension (Using the squares because for range you don’t need to do all this, you can just return range(0,9) ):

    >>> def display():
    ...     return [x**2 for x in range(9)]
    ... 
    >>> print display()
    [0, 1, 4, 9, 16, 25, 36, 49, 64]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create an empty spinner with no elements in it and add
How can I create empty variable that has the same data type of another
I want to create a class that can take different types of value in
This is the code in my view: echo $this->Form->create('Chart'); echo $this->Form->input('username', array('label'=>('Usernames List'), 'empty'=>('Select
How can I create an empty file at the DOS/Windows command-line? I tried: copy
What I want to do is to write a program that can generate large
I have a model called managers, I want to create a drop down list
I want to create an empty directory structure bar2 from a nonempty directory tree
I was wondering whether (and how) one can create empty c type arrays in
I want to create an HIMAGELIST for the list view. It actually needs to

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.