List slice


#1

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

first = suitcase[0:2] # The first and second items (index zero and one)
middle = suitcase[2:4] # Third and fourth items (index two and three)
last = suitcase[5:6] # The last two items (index four and five)
what 'em I doing wrong


#2

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'

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"]

You made mistake while slicing last line code.


#3

thank you but I knew this it just not working on my part


#4

Hey ,has the issue sorted? :slight_smile:


#5
last   = suitcase[4:]

Just have the wrong starting index.


#6

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