0

I have an array as shown marks = [1,2,3,5,6,7,8] I would like to understand the different between the following notations

marks[::-2], marks[None:None:-2] and marks[0:len(marks):-2]

I know there have been some attempts to answer the same question , But am not understanding those.

What i could gather from the other responses is that when the indices are omitted , start would default to 0 , end would default to len(marks) and step size would default to 1 .

Also omission of indices and giving None would yield same behavior . But i never observed it , especially in the above cases . For example if the above statement is true then

marks[::-2] = marks[0:len(marks):-2] which should give an empty list because you can never start from 0 index and reach an index len(marks) - 1 taking a negative step size of 2 but instead marks[::-2] is giving me [8, 6, 3, 1] what is the mechanism behind this ? Also please explain what None really means in this context

Edit

Below is the answer to the question which was mentioned as a comment in one of the answers given to a similar question although the answer wasn't accepted

maxint = 10000000 if step is None: step = 1 elif step == 0: raise ValueError('slice step cannot be zero') if start is None: start = maxint if step < 0 else 0 if stop is None: stop = -maxint if step < 0 else maxint

redeemed
  • 471
  • 7
  • 22
  • I don't think you can get a better explanation than answers to this question. [Understanding slicing](https://stackoverflow.com/questions/509211/understanding-slicing) – mx0 Oct 28 '22 at 08:08
  • The first answer in that link which you gave really didn't address my question to the point unless i overlooked something , but there was a part 2 answer to the same question (although not accepted) over here https://stackoverflow.com/questions/509211/understanding-slicing?page=2&tab=scoredesc#tab-top , please refer below code snippet which kind of answers what exactly is going on – redeemed Oct 29 '22 at 10:50
  • maxint = 10000000 if step is None: step = 1 elif step == 0: raise ValueError('slice step cannot be zero') if start is None: start = maxint if step < 0 else 0 if stop is None: stop = -maxint if step < 0 else maxint – redeemed Oct 29 '22 at 10:50
  • Can you edit this code into the question? and raise a reopen flag? – mx0 Oct 30 '22 at 16:33
  • @mx0 - I have edited and clicked on Reopen – redeemed Oct 31 '22 at 11:20
  • [This answer](https://stackoverflow.com/a/24713353/446792) deals with negative steps. For negative steps defaults are: `start = -1, stop = -len(string) - 1` – mx0 Oct 31 '22 at 12:34

0 Answers0