List Slices | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

List Slices

So, I am rolling through the course and I understand everything up until I got to this problem. Now, I think I understand it, but I need someones else view and explanation so that I can properly understand how this works. sqs = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print(sqs[7:5:-1]) I thought it would print >>>[49] However, IDLE shows >>>[49,36] So I know it starts at index 7 The 5 jumps ever fifth number The -1 goes the opposite direction. So if I count from the opposite direct Forward [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Backwards[81, 64, 49, 36, 25,......] So... is it just counting the 7th index 49 and going backwards 1? I am confused. LOL

19th Nov 2019, 11:54 PM
erica price
erica price - avatar
6 Answers
+ 4
You got it right, it starts with index 7 and goes backwards, it also stops at strictly before index 5, and since we're going in the opposite direction, then it will go from index 7, then 6, and then stops, so the two numbers that will be in the new list are index 7 and 6 of the original list. If, for example, it was sqs[7:3:-1], then it would start from 7, and stop strictly before 3, which is 4. Counting backwards: 7, 6, 5, 4, STOP! So the output will be [49, 36, 25, 16] If you still don't understand, feel free to ask for further explanation of where you were confused.
20th Nov 2019, 12:14 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
erica price Almost correct. The start position is always included, and the finish is not. So when going forwards, start position is included, end is not included, so just like you said: [9, 16, 25, 36] (because index 6 is not included, like you already said) The mistake you did is when going backwards, you included index 6 because it's the start, but you don't include index 2, so instead of [36, 25, 16, 9], the correct answer should be: [49, 36, 25, 16] (Because this time, start position is 6 (value 49), it should be included, end position is 2 (value 9), it should not be included)
20th Nov 2019, 2:39 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
erica price, it was just a coincidence in my example. In my example, the reason it outputs [49, 36, 25, 16] is not because we go back three steps, but because we stop at index three. Before we jump in to reversing the list, let just how it works normally: sqs[2:6:1], the course will start from index 2, moving forward one step, and stops at index 6 (remember, index 6 is NOT included), the here is the result: [4, 9, 16, 25] (we stop at index 6, which is 36, but we don't include it) Now going backwards, same concept: sqs[6:2:-1], the couse starts from index 6, going backwards one step, untill it reaches index 2 (again, index 2 not included) Thus the result: [36, 25, 16, 9] (the element that has index 2 is 4, but we don't include it)
20th Nov 2019, 1:07 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
@Aymane Boukrouh I see. Yeah that was a coincidence, so [1, 4, 9, 16, 25, 36, 49, 64] [2:6:-1] index two is 9 index 6 is 49 but since it stops at 6. Number 36 prints going back -1 prints[36, 25, 16, 9] because index two is 9 and that is where it starts/stops. Thank you soooo much!
20th Nov 2019, 2:32 AM
erica price
erica price - avatar
+ 1
Thank You Aymane!
20th Nov 2019, 2:08 PM
erica price
erica price - avatar
0
@Aymane Boukrouh. AHHHHH I get it now. I also used your example of [7:3:-1] which that made sense better than the example in the course. So it prints the 7th index which is 49, goes back -1 three times which stops at 16. Now the course 7th index which is 49, goes back -1, which is 36, but it can't go back 5 but 5 stops at 1, so I would assume it would print 0.??
20th Nov 2019, 12:55 AM
erica price
erica price - avatar