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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:10:49+00:00 2026-06-04T08:10:49+00:00

In C++, I can create a array like… int* a = new int[10]; in

  • 0

In C++, I can create a array like…

int* a = new int[10];

in python,I just know that I can declare a list,than append some items,or like..

l = [1,2,3,4]
l = range(10)

Can I initialize a list by a given size,like c++,and do not do any assignment?

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

    (tl;dr: The exact answer to your question is numpy.empty or numpy.empty_like, but you likely don’t care and can get away with using myList = [None]*10000.)

    Simple methods

    You can initialize your list to all the same element. Whether it semantically makes sense to use a non-numeric value (that will give an error later if you use it, which is a good thing) or something like 0 (unusual? maybe useful if you’re writing a sparse matrix or the ‘default’ value should be 0 and you’re not worried about bugs) is up to you:

    >>> [None for _ in range(10)]
    [None, None, None, None, None, None, None, None, None, None]
    

    (Here _ is just a variable name, you could have used i.)

    You can also do so like this:

    >>> [None]*10
    [None, None, None, None, None, None, None, None, None, None]
    

    You probably don’t need to optimize this. You can also append to the array every time you need to:

    >>> x = []
    >>> for i in range(10):
    >>>    x.append(i)
    

    Performance comparison of simple methods

    Which is best?

    >>> def initAndWrite_test():
    ...  x = [None]*10000
    ...  for i in range(10000):
    ...   x[i] = i
    ... 
    >>> def initAndWrite2_test():
    ...  x = [None for _ in range(10000)]
    ...  for i in range(10000):
    ...   x[i] = i
    ... 
    >>> def appendWrite_test():
    ...  x = []
    ...  for i in range(10000):
    ...   x.append(i)
    

    Results in python2.7:

    >>> import timeit
    >>> for f in [initAndWrite_test, initAndWrite2_test, appendWrite_test]:
    ...  print('{} takes {} usec/loop'.format(f.__name__, timeit.timeit(f, number=1000)*1000))
    ... 
    initAndWrite_test takes 714.596033096 usec/loop
    initAndWrite2_test takes 981.526136398 usec/loop
    appendWrite_test takes 908.597946167 usec/loop
    

    Results in python 3.2:

    initAndWrite_test takes 641.3581371307373 usec/loop
    initAndWrite2_test takes 1033.6499214172363 usec/loop
    appendWrite_test takes 895.9040641784668 usec/loop
    

    As we can see, it is likely better to do the idiom [None]*10000 in both python2 and python3. However, if one is doing anything more complicated than assignment (such as anything complicated to generate or process every element in the list), then the overhead becomes a meaninglessly small fraction of the cost. That is, such optimization is premature to worry about if you’re doing anything reasonable with the elements of your list.


    Uninitialized memory

    These are all however inefficient because they go through memory, writing something in the process. In C this is different: an uninitialized array is filled with random garbage memory (sidenote: that has been reallocated from the system, and can be a security risk when you allocate or fail to mlock and/or fail to delete memory when closing the program). This is a design choice, designed for speedup: the makers of the C language thought that it was better not to automatically initialize memory, and that was the correct choice.

    This is not an asymptotic speedup (because it’s O(N)), but for example you wouldn’t need to first initialize your entire memory block before you overwrite with stuff you actually care about. This, if it were possible, is equivalent to something like (pseudo-code) x = list(size=10000).

    If you want something similar in python, you can use the numpy numerical matrix/N-dimensional-array manipulation package. Specifically, numpy.empty or numpy.empty_like

    That is the real answer to your question.

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

Sidebar

Related Questions

I know that I can create an associative array like this: var MyAssocArray =
I know you can create an array like this: $a = array(); and append
hi i am new iphone programmer i want to know that can we create
I am just wondering how i can create a picturebox array like i could
I can create an array and initialize it like this: int a[] = {10,
How can I create an array of namespaces? And because it seems like a
I'm trying to create an array of strings that can be randomized and limited
What would be the best way to create an array that can have an
I need to create 2d array in c++. I can't do it by int
I know I can create an actionscript loop in a script block that can

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.