Python List and Dictionaries - List Slicing


The system is telling me that my first slice is ["sunglasses"] instead of ["sunglasses", "hat"] when my slice really looks exactly the same as the way it is telling me to change it to.

It seems like I am doing exactly what they are telling me to do, but it is still not accepting the code.

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
first  = suitcase[0:1]  # The first and second items (index zero and one)
middle = suitcase[2:3]  # Third and fourth items (index two and three)
last   = suitcase[4:5]  # The last two items (index four and five)


slicing is done wrong here.

First thing to know that Lists are 0-indexed.
what does that mean?
suppose.. x is a list.

x = ['a','b','c','d','e','f','g']

at index 0 -- > 'a'
at index 1 -- > 'b'
at index 2 -- > 'c'
at index 3 -- > 'd'
at index 4 -- > 'e'
at index 5 -- > 'f'
at index 6 -- > 'g'

for us 'a' is first element but for python its 0th element.
when we slice a list like this..

x[2:4] It will start slicing from index 2 to 3 excluding 4

so x[2:4] will be ["c","d"]


Got it, thank you! That was very helpful


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