#๐ list(range(5))[-5:0] should NOT be empty
65 messages ยท Page 1 of 1 (latest)
@humble kayak
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.
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 []
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].
so would it be [0:5]?
[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).
Like I mentioned previously, the indices of -5 and 0 select the same element when slicing a list of length 5. So since the length of the slice is zero when saying A[-5:0] it will result in an empty list.
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.
Slicing backward is allowed in python.
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[2, 1]
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
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;) ๐
This is because you're trying to do the opposite: slice forwards when the last index indicates you want to slice backwards.
No, the negative indexing is figured out separately for each one.
No it should be empty
ind -5 same as ind -1 and index 4
you're essentially doing arr[-1:-1]
!e print(list(range(5))[4:-1:-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[]
Man wtf
!e print(list(range(5))[0:5:-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[]
Ok
!e print(list(range(5))[0:4:-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[]
!e print(list(range(5))[::-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[4, 3, 2, 1, 0]
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[]
Now why doesn't that work
!e print(list(range(5))[4:0:-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[4, 3, 2, 1]
This should have worked then
OHHHHH
!e print(list(range(5))[-1:-5:-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[4, 3, 2, 1]
In stop spot
Just empty
!e print(list(range(5))[4:None:-1])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[4, 3, 2, 1, 0]
oh
You can use None as well
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).
it makes a slice object yes. Slice is a class
!e print(list(range(5))[slice(0, 4)])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
[0, 1, 2, 3]
cool
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.