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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:38:32+00:00 2026-06-15T11:38:32+00:00

I am new to Python and have read several tutorials on slicing however the

  • 0

I am new to Python and have read several tutorials on slicing however the examples I run in idle don’t seem to return what I expect it to. For example I have assigned the follow list to the variable a

a=[0,1,2,3,4,5,6,7,8,9]

Now I understand slicing to be as [number I want to include:number up to and don't want to include:step]

Hence if I do a[1], I would expect 1. If I do a[1:3], it would be 1,2

Now if I do a[-1], I get 9 BUT if I do a[-1:-5], I get nothing. All I see is []. why is that? I would expect to see 9,8,7,6

I am running Python 2.7 on Windows 7 Professional

  • 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-15T11:38:33+00:00Added an answer on June 15, 2026 at 11:38 am

    In this case, you would need to add in the step argument in order to get what you want:

    In [1]: a=[0,1,2,3,4,5,6,7,8,9]
    
    In [2]: a[-1:-5:-1]
    Out[2]: [9, 8, 7, 6]
    

    And if you want to save a bit more space, you can omit the first argument:

    In [3]: a[:-5:-1]
    Out[3]: [9, 8, 7, 6]
    

    The way that Python handles the ‘negative’ slices is that it adds then len of the object to the negative number. So when you say In a[-1:-5], it is basically saying a[(-1+10):(-5+10)], which equals a[9:5], and since start/end refers to all characters between the two (moving ‘forward’ through the list), it doesn’t return anything (hence your blank list). You can see this by doing something like:

    In [5]: a[-5:9]
    Out[5]: [5, 6, 7, 8]
    
    In [6]: a[5:9]
    Out[6]: [5, 6, 7, 8]
    

    You get the same result with the negative and positive indices, since -5 + 10 = 5.

    Providing the -1 step argument tells it to start at the first element but move backwards from the start position.

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

Sidebar

Related Questions

Im new using Python, now I have a problem using ElementTree and read out
I am new to Python as you might tell. I have read various documents
I'm pretty new to python, using python 2.7. I have to read in a
I use python 2.6 and have read many links about removing new line from
I am new to Python and have been studying its fundementals for 3 months
I'm new to python and have hit a problem with an SQL query I'm
hello im new to python and have been reading over the documentation and am
I am very new to python programming and have yet to buy a textbook
I am very new to Python and I have a json news feed that
I am new to python and I have a serious problem that I cannot

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.