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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:06:45+00:00 2026-06-06T01:06:45+00:00

You have x = [‘a’, ‘b’, ‘c’] y = [1, 2, 3] And want

  • 0

You have

x = ['a', 'b', 'c']
y = [1, 2, 3]

And want to insert list y at the beginning of x:

x = [1, 2, 3, 'a', 'b', 'c']

What is the optimal solution to do this in Python?

  • 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-06T01:06:47+00:00Added an answer on June 6, 2026 at 1:06 am
    >>> x = ['a', 'b', 'c']
    >>> y = [1, 2, 3]
    >>> x = y + x
    

    This simple solution runs twice as fast as the solution with deque for smaller input sizes:

    $ cat x1.py 
    for i in range(1000000):
        x = ['a', 'b', 'c']
        y = [1, 2, 3]
        x = y + x
    
    $ cat x2.py 
    from collections import deque
    for i in range(1000000):
        d = deque(['a', 'b', 'c'])
        d.extendleft(reversed([1, 2, 3]))
    
    $ time python x1.py
    
    real    0m1.912s
    user    0m1.864s
    sys     0m0.040s
    
    $ time python x2.py
    
    real    0m5.368s
    user    0m5.316s
    sys     0m0.052s
    

    However, it becomes slower for larger sizes of input:

    >python -m timeit -s "y = range(100000)" "x = list(xrange(10000000)); y+x"
    10 loops, best of 3: 229 msec per loop
    
    >python -m timeit -s "from collections import deque; y = range(100000)" "d = deque(xrange(10000000)); d.extendleft(reversed(y))"
    10 loops, best of 3: 178 msec per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css
**Have it working now. I forgot to populate the Array List. How embarrassing. I'm
Have converted devise new session from erb to Haml but doens't work, this is
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have a look at this url: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#column_chooser I have got the Column Chooser dialog
Have finally got a responsive site working (of a fashion). What I want to
have Googled the cr*p out of this one so apologies if the answer is
have a php code like this,going to convert it in to C#. function isValid($n){
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',

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.