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 94417

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:29:54+00:00 2026-05-10T23:29:54+00:00

Bear with me while I explain my question. Skip down to the bold heading

  • 0

Bear with me while I explain my question. Skip down to the bold heading if you already understand extended slice list indexing.

In python, you can index lists using slice notation. Here’s an example:

>>> A = list(range(10)) >>> A[0:5] [0, 1, 2, 3, 4] 

You can also include a stride, which acts like a ‘step’:

>>> A[0:5:2] [0, 2, 4] 

The stride is also allowed to be negative, meaning the elements are retrieved in reverse order:

>>> A[5:0:-1] [5, 4, 3, 2, 1] 

But wait! I wanted to see [4, 3, 2, 1, 0]. Oh, I see, I need to decrement the start and end indices:

>>> A[4:-1:-1] [] 

What happened? It’s interpreting -1 as being at the end of the array, not the beginning. I know you can achieve this as follows:

>>> A[4::-1] [4, 3, 2, 1, 0] 

But you can’t use this in all cases. For example, in a method that’s been passed indices.

My question is:

Is there any good pythonic way of using extended slices with negative strides and explicit start and end indices that include the first element of a sequence?

This is what I’ve come up with so far, but it seems unsatisfying.

>>> A[0:5][::-1] [4, 3, 2, 1, 0] 
  • 0 0 Answers
  • 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. 2026-05-10T23:29:54+00:00Added an answer on May 10, 2026 at 11:29 pm

    Ok, I think this is probably as good as I will get it. Thanks to Abgan for sparking the idea. This relies on the fact that None in a slice is treated as if it were a missing parameter. Anyone got anything better?

    def getReversedList(aList, end, start, step):     return aList[end:start if start!=-1 else None:step] 

    edit: check for start==-1, not 0

    This is still not ideal, because you’re clobbering the usual behavior of -1. It seems the problem here is two overlapping definitions of what’s supposed to happen. Whoever wins takes away otherwise valid invocations looking for the other intention.

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

Sidebar

Ask A Question

Stats

  • Questions 105k
  • Answers 105k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What I like most about nested functions is that it… May 11, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer I don't have an Outlook installation available to test this,… May 11, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer To be honest the remote debugging stored procedures from VS2005… May 11, 2026 at 8:46 pm

Related Questions

Greetings everyone. This is my first question here at stackoverflow so please bear with
I can proxy a single function in javascript by doing something like this (just
(Bear with me, I promise this gets to shebang and windows.) I have about
The code I'm working with has its own smart pointer implementation which does simple

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.