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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:13:19+00:00 2026-05-10T22:13:19+00:00

Code like this often happens: l = [] while foo: # baz l.append(bar) #

  • 0

Code like this often happens:

l = [] while foo:     # baz     l.append(bar)     # qux 

This is really slow if you’re about to append thousands of elements to your list, as the list will have to be constantly resized to fit the new elements.

In Java, you can create an ArrayList with an initial capacity. If you have some idea how big your list will be, this will be a lot more efficient.

I understand that code like this can often be refactored into a list comprehension. If the for/while loop is very complicated, though, this is unfeasible. Is there an equivalent for us Python programmers?

  • 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. 2026-05-10T22:13:19+00:00Added an answer on May 10, 2026 at 10:13 pm

    Warning: This answer is contested. See comments.

    def doAppend( size=10000 ):     result = []     for i in range(size):         message= "some unique object %d" % ( i, )         result.append(message)     return result  def doAllocate( size=10000 ):     result=size*[None]     for i in range(size):         message= "some unique object %d" % ( i, )         result[i]= message     return result 

    Results. (evaluate each function 144 times and average the duration)

    simple append 0.0102 pre-allocate  0.0098 

    Conclusion. It barely matters.

    Premature optimization is the root of all evil.

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

Sidebar

Ask A Question

Stats

  • Questions 63k
  • Answers 63k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If virtual machines will work for you, you can choose… May 11, 2026 at 10:29 am
  • added an answer The easiest way to go about it in Silverlight 2.0… May 11, 2026 at 10:29 am
  • added an answer Yes, it is still very relevant and an excellent resource. May 11, 2026 at 10:29 am

Related Questions

Code like this often happens: l = [] while foo: # baz l.append(bar) #
I'm still learning ASP.NET and I often see code like this throughout parts of
I often find I want to write code something like this in C#, but
I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
Many beginning programmers write code like this: sub copy_file ($$) { my $from =
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I have code like this: var newMsg = new Msg { Var1 = var1,
Say I have jquery code like this: html += '<div class=index>' + item.index +
I'm writing code like this, doing a little quick and dirty timing: var sw

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.