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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:43:02+00:00 2026-06-11T03:43:02+00:00

I recently started learning Python (my first programming language since using GW BASIC as

  • 0

I recently started learning Python (my first programming language since using GW BASIC as a kid). I’ve noticed that when adding bytes to a bytes object, each byte takes more time to add than the last; and by contrast, when adding integers to a list object, each integer takes the same amount of time to add as the last. The following program illustrates.

import time
import struct
time.clock() # for Windows

def time_list():
    print("adding 9,999,999 0s to one list 9 times:")
    a = []
    for i in range(9):
        start_time = time.clock()
        for j in range(9999999):
            a += [0]
        end_time = time.clock()
        print("loop %d took %f seconds" %(i, end_time - start_time))
    print()

def time_bytes_object():
    print("adding 99,999 pad bytes to a bytes object 9 times:")
    a = bytes()
    for i in range(9):
        start_time = time.clock()
        for j in range(99999):
            a += struct.pack('<B', 0)
        end_time = time.clock()
        print("loop %d took %f seconds" %(i, end_time - start_time))
    print()

time_list()
time_bytes_object()

What is it about the bytes object (or the struct.pack function) that makes adding bytes take increasing amounts of time? Or is there a faster way to collect a bunch of bytes than the way used in my example?

Thanks for any help,

Victor

  • 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-11T03:43:04+00:00Added an answer on June 11, 2026 at 3:43 am

    Byte strings (and Unicode strings) in Python are immutable, whereas lists are mutable.

    What this means is that every append (+=) done on a byte string must make a copy of that string; the original is not modified (though it will be garbage-collected later). In contrast, the append method of list (also used by +=) will actually modify the list.

    What you want is the bytearray type, which is a mutable type functioning much like a list of bytes. Appending to a bytearray takes (amortized) constant time, and it is easily converted to and from a byte string.

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

Sidebar

Related Questions

I have recently started learning Ruby, as my first programming language. I feel comfortable
I do Java programming and recently started learning Python via the official documentation. I
I've recently started learning Python and wrote a little script that informs me when
I've recently been learning python and I just started playing with networking using python's
I've recently started learning XS using perlxstut and the tutorial suggests that I create
I'm used to python and django but I've recently started learning java. Since I
I have only started learning python recently. I would still be considered a beginner.
I have only started learning Python recently. Let me explain what I am trying
Recently I have started learning rails and was a little surprised that the default
I have recently started learning Python and I have 2 questions relating to modules.

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.