Some clarification to the use of the [-1] or [-i]


#1

I was wondering if someone could give me some clarification to the use of the [-1] or [-i] I keep seeing in these solutions. I don't understand how it works. My first instinct was to somehow use len(text) - i to reverse it but I wasn't having success with that and now that I see these solutions I am lost as to how it works, as I would have never thought of that.


No need to transform it to list
#2

I asked the exact same thing up above.
Even though all the options in the Q&A are also correct, they all include things us beginners have never even heard of. I have a feeling @sircabaj above has the answer that the test was expecting - I just tested it and it works great, and so much simpler than all the other answers given on the forum.

text[-i] is something called slicing. We haven't been introduced to it in the tutorials, and it's difficult do use something you don't even know exists.

Slice explanation as per Glossary:

Slice

A Pythonic way of extracting "slices" of a list using a special bracket notation that specifies the start and end of the
section of the list you wish to extract. Leaving the beginning value blank indicates you wish to start at the beginning
f the list, leaving the ending value blank indicates you wish to go to the end of the list. Using a negative value
references the end of the list (so that in a list of 4 elements, -1 means the 4th element). Slicing always yields another
list, even when extracting a single value.

Example
>> # Specifying a beginning and end:
>> x = [1, 2, 3, 4]
>> x[2:3]
[3]

>> # Specifying start at the beginning and end at the second element
>> x[:2]
[1, 2]

>> # Specifying start at the next to last element and go to the end
>> x[-2:]
[3, 4]

>> # Specifying start at the beginning and go to the next to last element
>> x[:-1]
[1, 2, 3]

>> # Specifying a step argument returns every n-th item
>> y = [1, 2, 3, 4, 5, 6, 7, 8]
>> y[::2]
[1, 3, 5, 7]

>> # Return a reversed version of the list ( or string )
>> x[::-1]
[4, 3, 2, 1]

>> # String reverse
>> my_string = "Aloha"
>> my_string[::-1]
"aholA"

#3

Thank you that helped a lot, I vaguely remember a brief section on slices but I'm 90% certain it didn't cover using negatives in slices.


#4

Easier comes with a price. We don't learn if we don't explore. Settling on the first thing that comes to mind may give a working solution, but then what? What has been learned? Is treating a string as a mutable object the right approach? Not really.

Strings are immutable and should be respected as such. Yes, iterate over the string instead of a list. But a list is a better container to mutate. It makes logical sense and respects the nature of strings. The final result can be joined to form a new string.


#5

After progressing through the Python course more I eventually got to the topic of Slices which covered essentially all of this. So for anyone else reading this or still confused hang in there and it will be covered in a bit.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.