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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:29:20+00:00 2026-06-17T13:29:20+00:00

Here is some code that I wrote using Python: from math import sqrt abundant_list

  • 0

Here is some code that I wrote using Python:

from math import sqrt
abundant_list = []

for i in range(12,28123+1):
    dividor_list = [1]
    for j in range(2, int(sqrt(i))+1):
        if i%j == 0:
            dividor_list.extend([i/j,j])
    if sum(dividor_list) > i:
        abundant_list.append(i)

print abundant_list

As you can see, the code is really trying to be efficient as much as possible.

There is any difference if I use list.append twice, or list.extend just once?
I know it can be minor differences, but I would really like to know that 🙂

  • 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-17T13:29:21+00:00Added an answer on June 17, 2026 at 1:29 pm
    import timeit
    
    def append2x(foo):
        foo.append(1)
        foo.append(1)
    
    def extend_lst(foo):
        foo.extend([1,1])
    
    def extend_tup(foo):
        foo.extend((1,1))
    
    
    l1 = []
    l2 = []
    l3 = []
    
    print timeit.timeit('append2x(l1)',setup = 'from __main__ import append2x,l1')
    print timeit.timeit('extend_lst(l2)',setup = 'from __main__ import extend_lst,l2')
    print timeit.timeit('extend_tup(l3)',setup = 'from __main__ import extend_tup,l3')
    

    Here’s a simple benchmark. My results (os-X, 10.5.8, core2duo, FWIW):

    0.520906925201  #append
    0.602569103241  #extend-list
    0.357008934021  #extend-tuple
    

    And the same ordering of the results my linux box (Ubuntu, x86-64 core i7):

    0.307395935059  #append
    0.319436073303  #extend-list
    0.238317012787  #extend-tuple
    

    To me, this says that extend is quicker than append, but that creating a list is relatively expensive compared to creating a tuple


    EDIT

    Pointed out in the comments below, because of the immutability of tuples, the interpreter can optimize the creation of the tuple out (it creates the tuple once and re-uses it over and over). If we change the code to:

    def extend_lst(foo):  
        v = 1
        foo.extend([v,v]) 
    
    def extend_tup(foo):
        v = 1
        foo.extend((v,v))
    

    The timings are virtually identical:

    0.297003984451  #append
    0.344678163528  #extend-list
    0.292304992676  #extend-tuple
    

    Although tuple still consistently beats the list version and barely edges out the append version for all of the trials I have done.

    One thing that I’m taking away from this is that if you’re iterating over an object that consists of all literals, choose a tuple over a list. If it doesn’t consist entirely of literals, then it really doesn’t matter whether you choose list or tuple.

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

Sidebar

Related Questions

I have some code that is using SyncEnumerator. As you can see here ,
I have some code here that uses bitsets to store many 1 bit values
I've got some code here that works great on IPv4 machines, but on our
So I have some PHP code that looks like: $message = 'Here is the
Here's some example code: class Obj attr :c, true def == that p '=='
Just some example code here, but I have lists of strings that I want
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
I have some python code for Google App Engine that responds with the string
I am trying to refactor some code I wrote that uses IndexedDb. Ideally what
I wrote some python code on my mac and how I have to transfer

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.