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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:14:00+00:00 2026-06-05T23:14:00+00:00

What is the fastest way to extend the front of an array in python?

  • 0

What is the fastest way to extend the front of an array in python? Lets say I’ve got 2 arrays:
a and b. I want to make the fastest way of a = b+a (b should not change).

My small benchamarks:

test 1:

a,b = [],[]
for i in range(0,100000):
    a.append(i)
    b.append(i)

def f(a,b):
    for i in range(0,100):
        a=a+b

import cProfile
cProfile.run('f(a,b)')

time: ~12 s

test 2:

a,b = [],[]
for i in range(0,100000):
    a.append(i)
    b.append(i)

def f(a,b):
    for i in range(0,100):
        a[0:0] = b

import cProfile
cProfile.run('f(a,b)')

time: ~1.5s

test3:

a,b = [],[]
for i in range(0,100000):
    a.append(i)
    b.append(i)

lenb = len(b)
def f(a,b):
    for i in range(0,100):
        b.extend(a)
        # do something with b
        b = b[:lenb]

import cProfile
cProfile.run('f(a,b)')

time: ~0.4s

But i think It should be faster, because lists concatenation should be made as change of few underlying pointers.
And the following code is the fastest one, but changes b, not a (SO IT IS NOT GOOD FOR OUR PURPOSE):
test “WRONG”:

a,b = [],[]
for i in range(0,100000):
    a.append(i)
    b.append(i)

def f(a,b):
    for i in range(0,100):
        b.extend(a)

import cProfile
cProfile.run('f(a,b)')

time: ~0.13s

So theoretically there should be a way to extend front of a in time of test “WRONG”.

  • 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-05T23:14:01+00:00Added an answer on June 5, 2026 at 11:14 pm

    The absolute fastest way would be to use a collections.deque which is optimised for exactly this use, and has methods called .appendleft and .extendleft to make the code nice and readable – appendleft does exactly what it says on the tin (ie, it appends to the left of the deque), and extendleft does the equivalent of:

    def extendleft(self, other)
        for item in other:
            self.appendleft(c)
    

    so, a = b+a would be spelled:

    a.extendleft(reversed(b))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fastest way to uniqify a list in Python without preserving order? I saw many
What is the fastest way to compare a string with an array of strings
What's the fastest way to populate an array with the numbers 1-100 in PHP?
What's the fastest way, in ubuntu 11.10, to take a screenshot with python and
What's a fastest way to implement array subtraction? For example: array a1 = [1,
What is the fastest way to sort an array of whole integers bigger than
What would be the fastest way to get the numerical format ( NOT THE
what would be the fastest way to merge a list of numpy arrays into
What is the fastest way to convert a simple array to an associative array
What's the fastest way to compare if the keys of two arrays are equal?

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.