I highly recommend rewording this section, because the current description of “omitting the final index” is confusing and I had to change and run it a few times to understand what it was doing. Or maybe an example with more than just four elements.
List slicing works by including the element at the start index and excluding the element at the end index. For example, lst[1:5] would include the elements at indexes 1, 2, 3, and 4. The same goes for slices that involve negative indices. lst[-4:-2] would include the elements at indexes -4 and -3.
You can take a look at the documentation for more information.
Side note: you can format code in your posts using the </> button.
Thanks a lot for the responses!
I thought that was the way it works, so the course material should be corrected I think. I have bolded what I think is wrong.
When we write lst[:3] we list the indexes “0”, “1” and “2”, omitting the last one, while the course material says “0” is omitted, in which case it wouldn’t display the first element of the list and it would display the last.
for example in this code:
colon after number 2 ([2:]), the outcome shows, first 2 elements were sliced. but for -2, the outcome shows last 2 elements were kept. I am totally lost. why the colon is placed same, but outcomes are different?
It shows where index 2 captures the rest of the list (colon after) , and where index -2 captures a slice.
list[start:] => slice the list from start to last index
When there is no step value, the default is left, from and including start index, to right, with step value 1. When end is omitted, the end is index[len(lst)].
A list has two indices per element. The positive indices start at zero and increase to the end of the list, from left to right. The negative indices start at -1 and decrease to the beginning of the list, from right to left.
To find the negative index of any element we just need to subtract the length of the list. Say the length is 6.
+ -
0 - 6 => -6
...
5 - 6 => -1
To find the positive index we add the length…
- +
-6 + 6 => 0
...
-1 + 6 => 5
To answer your question, it doesn’t matter which index we use as the start, the direction is from left to right. We’ve only chosen our index from the right .
fruits[:n] - what does the “n” represent. Is it calling for the first letter “n” in the list?
Is it being used as a variable? I am confused? I understand positive and negative integers and how they are called in regards to the list.
I put this in pythonfiddle to see what output it would return. It didn’t return anything. So just so I am clear n is the representation of a variable that has a default value of 0. Yet it can be replaced with any number value positive or negative in the list. As a stand alone though it’s default value is index 0?
In this representation, [:n] the first position does default to zero when left vacant, as above. n must be defined, but can as you observe be either positive or negative. Since a slice is virtual, any value will be valid, whether it renders anything or not.
The first element in the list is always 0. Does this change? I ran across some lessons where it seems like the first element was being represented as “1” and the zero was the entire list. This has me confused because I want to count from left to right zero every time.