#๐Ÿ”’ list(range(5))[-5:0] should NOT be empty

65 messages ยท Page 1 of 1 (latest)

humble kayak
#
print(list(range(5))[-5:-1])
[0,1,2,3]

but

print(list(range(5))[-5:0])
[] # unexpected, it should be [0,1,2,3,4]
lime flameBOT
#

@humble kayak

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sterile pilot
# humble kayak ```py print(list(range(5))[-5:-1]) [0,1,2,3] ``` but ```py print(list(range(5)...

I don't think it's unexpected. An index of -5 would indicate the first element in the converted list, right? The 0 to the right of the colon would indicate the end index. In this case, because of the list length, -5 points to the same index as 0. So print(list(range(5))[-5:0]) would be the same as saying print(list(range(5))[-5:-5]) or print(list(range(5))[0:0]), and all of these end up printing []

humble kayak
#

Zero to the right of the colon would indicate that the end index is -1 rather than 0.

#

Because A[i:j] takes all elements from A[i] to A[j-1].

vast wasp
#

0 is the first index

#

so if that's the end index, no elements are included

rocky folio
#

so would it be [0:5]?

humble kayak
#

[0,1,2,3,4]

#

A[0:5] and A[-5:0] should be [0,1,2,3,4].
Because A[-5:0] is the same as subtracting 5 from both index in A[0:5] (I think).

sterile pilot
humble kayak
#

print(list(range(5))[-3:0]) also produces []

sterile pilot
# humble kayak `print(list(range(5))[-3:0])` also produces `[]`

So do any of the following:

print(list(range(5))[-4:0])
print(list(range(5))[-2:0])
print(list(range(5))[-1:0])

I would guess it's because you're trying to slice backwards. In other words the start index of the slice shouldn't be past the end index or it would just result in an empty slice.

humble kayak
#

Slicing backward is allowed in python.

hoary star
#

Yes, but you must specify it.

#

!e print([0,1,2,3,4][-3:0:-1])

lime flameBOT
sterile pilot
#

Correction to what I said: it's because you're trying to slice backwards when the default is forwards if you leave out that third index

humble kayak
#

It is also empty print(list(range(5))[-4:-1:-1]) ( Thank you. i would revisit this tomorrow, it is 3:20 AM now, goto sleep;) ๐Ÿ™‚

sterile pilot
west holly
celest dragon
#

No it should be empty

#

ind -5 same as ind -1 and index 4

#

you're essentially doing arr[-1:-1]

dawn current
#

!e print(list(range(5))[4:-1:-1])

lime flameBOT
dawn current
#

Man wtf

dawn current
#

!e print(list(range(5))[0:5:-1])

lime flameBOT
dawn current
#

Ok

exotic forge
#

!e print(list(range(5))[0:4:-1])

lime flameBOT
dawn current
#

!e print(list(range(5))[::-1])

lime flameBOT
dawn current
#

Ok so that works

#

!e print(list(range(5))[:-1:-1])

lime flameBOT
dawn current
#

Now why doesn't that work

exotic forge
#

!e print(list(range(5))[4:0:-1])

lime flameBOT
exotic forge
#

here

#

It's just reversed

dawn current
#

OHHHHH

exotic forge
#

!e print(list(range(5))[-1:-5:-1])

lime flameBOT
dawn current
#

-1 is last I'm so dumb

#

so you have to use None or empty

exotic forge
#

Where?

#

In slicing syntax?

dawn current
#

In stop spot

exotic forge
#

Just empty

dawn current
#

!e print(list(range(5))[4:None:-1])

lime flameBOT
exotic forge
#

oh

dawn current
#

You can use None as well

exotic forge
#

Seems because it works like a slice function

#

!docs slice

lime flameBOT
#

class slice(stop, /)``````py

class slice(start, stop, step=None, /)```
Return a [slice](https://docs.python.org/3/glossary.html#term-slice) object representing the set of indices specified by `range(start, stop, step)`. The *start* and *step* arguments default to `None`.

Slice objects are also generated when [slicing syntax](https://docs.python.org/3/reference/expressions.html#slicings) is used. For example: `a[start:stop:step]` or `a[start:stop, i]`.

See [`itertools.islice()`](https://docs.python.org/3/library/itertools.html#itertools.islice) for an alternate version that returns an [iterator](https://docs.python.org/3/glossary.html#term-iterator).
dawn current
#

it makes a slice object yes. Slice is a class

exotic forge
#

!e print(list(range(5))[slice(0, 4)])

lime flameBOT
exotic forge
#

cool

lime flameBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.