8. Omitting Indices


#1



https://www.codecademy.com/courses/python-beginner-en-KAgt5/2/2?curriculum_id=4f89dab3d788890003000096#


Hello this is what I've done.I checked other topics and I know that using print my_list[::2] will work but I want to know why doesn't my code work? :confused:
Oops, try again.
Make sure to omit the appropriate indices in your list slice. Check the Hint if you need help!

I saw that my code has the correct outputs: :worried:
Outputs of my code:
[1, 3, 5, 7, 9]
None
Outputs of correct code:
[1, 3, 5, 7, 9]
None


I just don't know why my code doesn't work.


my_list = range(1, 11) # List of numbers 1 - 10

# Add your code below!
print my_list[0:11:2]


#2

Would you say that you are omitting any indices? If so where/how? If not, then you know what's up already!


#3

My guess would be that the scoring engine is looking for you to use [::2] so even though your code is correct and provides the exact same output, it's not what they were looking for. That doesn't necessarily mean that it is wrong or bad, just not what they are scoring on.


#4

Yes I am. :confused:
I'm mean not omitting something from the exact list but printing what we need and want. :wink:
and I know that printing the part that we want is correct because I found it in another topic and I tryed it and it worked. :nerd:
But here my question is what is the difference between my_list[::2] and my_list[0:11:2]?:thinking:
Anyway thanks for asking and wanting to help. :upside_down:


#5

Thank you. :green_heart:
yep maybe you are right. :blush:


#6

Yeah, you printed the right value, but the exercise was about omitting arguments to the slice.
Python uses default values based on what arguments you do provide, omitting arguments means that these defaults get used which is useful when you just "want it all" or some such and don't want to clutter the expression with figuring out what the length is.

It's also not possible to specify the value that comes before 0, because -1 refers to the last element - so you would have a hard time telling it to iterate backwards and include the whole thing. You would need it to use its default STOP index.


#7

[::2] starts at the very beginning and goes to the end using a stride of 2. [0:11:2] goes from the very beginning to the 11th index using a stride of 2. In your use case here they are completely identical in what they output. Really the only difference is how you did it specified a beginning and end point.


#8

Thank you again. :cherry_blossom:
I got it I think :grin:


#9

Thank you.:grinning: :purple_heart:


#10

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