List Slicing


Im not sure what I am doing wrong... Any help please?

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first  = suitcase[0:2]  # 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"]


Thank you. It helped me alot. :smile:


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