Isn't slicing a list meant to start with 0?

hey there folks
enjoying myself immensely with the quests and am learning how to slice a list :slight_smile:
was wondering if someone could let me know why I got an error for the following quest:
I was meant to:
Select the first four elements from row_1 using a list slicing syntax shortcut. Assign the output to a variable named first_4_fb

so I put in
‘’‘first_4_fb = row_1 [0:3]’’’

when submitting I was told my slice was too short and only accepted when I changed the values to [0:4]
doesn’t 0 actually count as the first element in a list, meaning that if you want the first 4 you need to count zero as the first element?

thanks in advance

You are right, list indexing starts with 0! With list slicing, XX[a:b] means that we start with index a, and go up to but not including index b. That’s why [0:3] was too short, because it’s getting indices 0, 1, 2 and not including index 3.

It takes a little bit to get used to, but it’s probably done that way because list indexing is zero-based. It makes it nice, though, when you use the other notation to get the "first n" of a list: XX[:n]. Functionally, both XX[0:n] and XX[:n] work the same (but I much prefer the 2nd version!).

1 Like

oh yeah of course, thanks for clearing it up for me April :slight_smile: