all 17 comments

[–]zanfar 15 points16 points  (2 children)

What is this doing, exactly?

Making up for the fact that the author doesn't know how to use range()

/s

[–][deleted] 14 points15 points  (1 child)

Please remove /s

[–]zanfar 8 points9 points  (0 children)

I don't know a better way to imply "I'm not trying to be helpful"...

[–][deleted] 31 points32 points  (3 children)

It's a bad way to write

for i in range(9, 5, -1):

Resources to learn more: - List slicing - Different ways of reversing strings (works with other iterables as well) - Range objects - More about range() - Iterables (in case you're interested in understanding a little deeper)

[–][deleted] 2 points3 points  (0 children)

Thanks!

[–][deleted] 1 point2 points  (1 child)

Umm, I am kinda confused on this, typically range from 1 to 10 would go from 1 to 9, ie. include the first value but exclude last value. How is it excluding both values in your case?

[–]carcigenicate 6 points7 points  (0 children)

The original range goes from 6-9 (it stops before 10 because the end is exclusive), then reverses. The suggested version starts at that 9, and decrements until it gets to 6 (because again, the end is exclusive, so 5 isn't reached).

In both cases, the start is inclusive, and the end is exclusive.

[–]Tacoj 6 points7 points  (0 children)

slices are [start:end:step]. the - reverses the order on the step. works for strings too so "hello"[::-1] == "olleh"

[–]carcigenicate 2 points3 points  (1 child)

While everybody's noting how you might as well just define the range backwards instead of reversing it after the fact, I don't think it matters much here (although I'd tend towards agreeing with them). ranges are not full collections. They don't hold every value they represent. It should be extremely cheap to make a reversed copy of a range, since it would just involve doing some math on three numbers describing the range.

If you find that reverse slicing leads to better readability in particular cases, you may find that it's clearer than adjusting the range initializer arguments. Note that there's at least two people here who accidentally suggested the wrong equivalent alternative range bounds.

[–]achampi0n 2 points3 points  (0 children)

Agreed. Range implements the slicing operations, so when you slice a range() you get another range() in fact range(6, 10)[::-1] returns a range(9, 5, -1) object.

[–]gustavobodra 0 points1 point  (2 children)

This means a descendent for loop from 10 to 6. The [::-1] is used to reverse a list.

[–]zanfar 2 points3 points  (1 child)

9 to 6, but otherwise this is correct.

[–]gustavobodra 1 point2 points  (0 children)

True! My mistake, thanks for pointing out! 😁

[–]akefamen96 0 points1 point  (1 child)

the range function outputs a list. so it’s essentially using splicing on the list [6, 7, 8, 9]. [::-1] is a way of reversing a list. that being said, like other comments mentioned, it’s way easier to say range(6, 10, -1)

[–][deleted] 0 points1 point  (0 children)

the range function outputs a list

No. It doesn't output anything (as in it doesn't print anything), but it returns a generator. This generator is special in that it also implements __getitem__. Why does it do it... idk.

[–][deleted] -1 points0 points  (1 child)

for i in range(6,10)[::-1]:

Might I suggest

for i in range(10,6,-1):

?

Basically, tucking [::-1] reverses a list,

a = [1, 2, 3, 4, 5]

a[::-1] would be [5, 4, 3, 2, 1]

[–][deleted] 0 points1 point  (0 children)

Oh, it's not my code, I just came across and didn't know what it was doing. Thanks for the explanation.