What indexes do I use to access the elements I want?

Question

What indexes do I use to access the elements I want?

Answer

There’s a simple key to using list slices, and that’s remembering this rule:
[inclusive : exclusive]
All that means is that the first index we write is included in the slice, and the second is excluded. Because of this, we start with the index we want, and end one index after the last index we want.
If I have my_list = [0, 1, 2, 3] and want the numbers 2 and 3, which are in indexes 2 and 3 respectively, I would start with the index I want, and end one index after the last one I want, like this:
my_slice = my_list[2:4]
Even though there is no 4th index in our list, it’s safe to write it like that because it stops before it gets to that index!

6 Likes

Or you can simply use my_slice = my_list[2:], which means start at index 2 and take all items to the end.

13 Likes

May be a bit of silly question, but just curious. The example this use in this exercise is as follows:
letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
slice = letters[1:3]
print slice
print letters

With letters appearing in red font as expected. Slice and Print appear in white font as if they are from the same library (if that is the correct term here). So I figured it should work on my code for the exercise and got the following error: “SyntaxError: invalid syntax”

I was able to easily fix it by removing the word slice, but I’m interested because even in my code the font color was the same as print. My code was as follows:

suitcase = [“sunglasses”, “hat”, “passport”, “laptop”, “suit”, “shoes”]

The first and second items (index zero and one)

first = suitcase[0:2]

Third and fourth items (index two and three)

middle = slice suitcase[2:4]

The last two items (index four and five)

last = slice suitcase[4:]

4 Likes

Hi Ryan,

in your code you assigned the first two elements correctly from list “suitcase” to list “first”.
BUT why did you add the variable “slice” when you created lists “middle” and “last” respectively?
Do you have intended to also assign the letters ‘b’, and ‘c’ to the respective lists?
Then you should have written

middle = [slice[0], slice[1], suitcase[2], suitcase[3]]
middle
[‘b’, ‘c’, ‘passport’, ‘laptop’]

last = [slice[0], slice[1], suitcase[4], suitcase[5]]
last
[‘b’, ‘c’, ‘suit’, ‘shoes’]

or

middle = slice, suitcase[2:4]
middle
([‘b’,‘c’], [‘passport’, ‘laptop’])

last = slice, suitcase[4:6]
last
([‘b’, ‘c’], [‘suit’, ‘shoes’])

Keep in mind to add a comma between slice and suitcase in the latter example.

1 Like

Is it possible to concatenate the lists ?

A post was split to a new topic: Video - slicing

I have a question. Now, I’ve read @parzivalkei answer about how to use the list to get the last values of the suitcase list. He said that one option to obtain that was using [4:] which I never thought of and seems like the optimal solution.

The thing is that I used the [4:6] to obtain the values 4 and 5 from that list as it excludes the last index. The thing is that that list only has 5 values. Although the programm said that my solution was correct I was wondering what is the best practice.

I’m guessing is the first one, but it is always good to know the point of view of an expert. I’m basing this opinion in the fact that if we have a tremendous list and we want to do a slice to get from x term to the last term (which imagine is 405684) it might be best to do list[x:] instead of list[x:405685]. I’m going to use this man’s technique. In case it’s not like that, please tell me so.

Have a gorgeous day!

Hi @pa_u_los.

Just note that you’re replying to a query which is quite dated (Feb 2019) and therefore unlikely to be too helpful. As for list slicing I’d say it’s just a case of choosing whichever makes you code clearer and easier to read.

I’m certainly no expert but if you wanted to slice your list from index 10 to the very end I’d say the lst[10:] method is nicer since it’s unambiguous. Just aim for whatever makes it easier for the next person to read.

2 Likes