Https://www.codecademy.com/courses/learn-python/lessons/advanced-topics-in-python/exercises/omitting-indices

Dear fellow colleagues,

https://www.codecademy.com/courses/learn-python/lessons/advanced-topics-in-python/exercises/omitting-indices

The second print statement is my attempt,the first one is the solution,why am l wrong,l am looking to increase by 1 so starting 1 then 3 then 5,please explain

Thank you

In the instructions it’s asking you to specify a stride, which is the third (optional) parameter in your slice.[beg: end: stride] A stride of 5 will give you the numbers in your slice skipping by 5, a stride of -1 will give you the numbers backwards (incrementing by -1) etc.

So here my stride is 1 and l am starting with 1 so l skip 1 and get 3 then 5 and so on,this was my logic,please correct me

Thank you

list = [0,1,2,3,4,5,6]

list[0:6:1] so if you’re beginning is 0, your end is 6 exclusive, and your stride is 1: you will increment by 1, stopping before 6.
[0, 1, 2, 3, 4, 5]

list[0:6:2] if you’re beginning is 0, your end is 6 exclusive, and your stride is 2: you will increment by 2, stopping before 6.
[0, 2, 4]

list[0:6:3]if you’re beginning is 0, your end is 6 exclusive, and your stride is 3: you will increment by 3, stopping before 6.
[0, 3]

list[::1]
outputs: [0, 1, 2, 3, 4, 5]
list[::2]
outputs: [0, 2, 4, 6]
list[::3]
outputs: [0, 3]

1 Like

@jagmeetsond589256300 for future reference, it’s always useful to play around in your terminal with python to see what outputs come out. I do it all the time when something isn’t working like I thought it would!

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