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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:41:36+00:00 2026-06-12T19:41:36+00:00

This is a very simple question, but I haven’t seem to be able to

  • 0

This is a very simple question, but I haven’t seem to be able to find a satisfactory answer for it.

What is the best way, in Python, make the last item of a list become the first one “pushing” the rest of the list.

Something that does:

>>> a=[1,2,3,4]
>>> a[?????]
[4, 1, 2, 3]

I know I can always play with len, list concatenation…

>>> a=[1,2,3,4]
>>> [a[len(a)-1]] + a[0:len(a)-1]
[4, 1, 2, 3]

But that doesn’t look right… “Pythonic“, if you may

Thank you in advance.

  • 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-12T19:41:38+00:00Added an answer on June 12, 2026 at 7:41 pm

    Slicing is a little smarter than that; you can use negative indices to count from the end:

    a[-1:] + a[:-1]
    

    Demo:

    >>> a=[1,2,3,4]
    >>> a[-1:] + a[:-1]
    [4, 1, 2, 3]
    

    This works for an arbitrary number of elements to be moved to the front:

    >>> a[-2:] + a[:-2]
    [3, 4, 1, 2]
    

    Using slicing like this is comparable in speed to using .insert() + .pop() (on a short list):

    >>> timeit.timeit('a[-1:] + a[:-1]', 'a=[1,2,3,4]')
    0.59950494766235352
    >>> timeit.timeit('a.insert(0,a.pop(-1))', 'a=[1,2,3,4]')
    0.52790379524230957
    

    but wins hands down if you need to shift more than one element:

    >>> timeit.timeit('a[-2:] + a[:-2]', 'a=[1,2,3,4]')
    0.58687901496887207
    >>> timeit.timeit('a.insert(0,a.pop(-1));a.insert(0,a.pop(-1))', 'a=[1,2,3,4]')
    1.0615170001983643
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is another probably very simple question, but I haven't been able to find
This looked like a very simple question but I couldn't find the answer. How
This is a very simple question, but I can't seem to find something about
This question might appear very simple, but i haven't found an answer yet, so
This is a very simple question, but one I can't find an answer to.
I have a very simple question, but can't seem to find a strait answer
This may be a very simple question, but I haven't found an answer to
This may seem like a very simple question, however I haven't been able to
This is a very basic question - but I haven't been able to find
I have a very simple question but cannot find a simple answer. I have

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.