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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:27:09+00:00 2026-05-26T07:27:09+00:00

I have written basic python snippets to first insert values in a list and

  • 0

I have written basic python snippets to first insert values in a list and then reverse them. I found that there was a huge difference of speed of execution between insert and append methods.

Snippet 1:

L = []
for i in range(10**5):
 L.append(i)
L.reverse()

Time taken to execute this :

real    0m0.070s
user    0m0.064s
sys         0m0.008s

Snippet 2:

l = []
for i in range(10**5):
 l.insert(0,i)

Time taken to execute:

real    0m5.645s
user    0m5.516s
sys         0m0.020s

I expected the snippet 2 to perform much better than snippet1, since I am performing the reverse operation directly by inserting the numbers before. But the time taken says otherwise. I fail to understand why the latter method takes more time to execute, even though the method looks more elegant. Does any one have any explanation for this?

  • 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-26T07:27:09+00:00Added an answer on May 26, 2026 at 7:27 am

    Note that your results will depend on the precise Python implementation. cpython (and pypy) automatically resize your list and overprovision space for future appends and thereby speed up the append furthermore.

    Internally, lists are just chunks of memory with a constant size (on the heap). Sometimes you’re lucky and can just increase the size of the chunk, but in many cases, an object will already be there. For example, assume you allocated a chunk of size 4 for a list [a,b,c,d], and some other piece of code allocated a chunk of size 6 for a dictionary:

    Memory  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
           |a b c d| | dictionary |
    

    Assume your list has 4 elements, and another one is added. Now, you can simply resize the list to size 5:

    Memory  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
           |a b c d e| dictionary |
    

    However, what do you do if you need another element now?

    Well, the only thing you can do is acquire a new space and copy the contents of the list.

    Memory 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
                    | dictionary |a  b  c  d  e  f |
    

    Note that if you acquire space in bulk (the aforementioned overprovisioning), you’ll only need to resize (and potentially copy) the list every now and then.

    In contrast, when you insert at position 0, you always need to copy your list. Let’s insert x:

    Memory  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    orig   |a b c d| |dictionary|
    after  |x a b c d|dictionary|
    

    Although there was enough space to append x at the end, we had to move (not even copy, which may be less expensive in memory) all the other values.

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

Sidebar

Related Questions

I have written a very basic jquery script that is true theorically but not
I have written a program on Visual Basic. In the debug folder, there are
I have written a basic Windows Form app in C# that has an embedded
I have written a basic Application for windows mobile 5. It is using visual
I have written a basic script for the mail functionality. I am trying to
I have a big application written in Visual Basic 6 and I need to
Although I know the basic concepts of binary representation, I have never really written
Does anyone have a solution for a basic, compact Finite state machine/automata written in
I have written an AIR Application that downloads videos and documents from a server.
I have an open source project written in python , it has some Forms

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.