Help me in listing


suitcase = [“sunglasses”, “hat”, “passport”, “laptop”, “suit”, “shoes”]

The first and second items (index zero and one)

first = suitcase[0:2]

Third and fourth items (index two and three)

middle = suitcase[2:4]

The last two items (index four and five)

last = suitcase[4:6]

why its accepting 6 indices as according to computer there are only 5 index in suitcase ?


list slicing involves a start and stop value, but only for values lesser then (<) stop value. so 5 is the highest value used

sort of like this:

x = "hello world"
y = 0
while y < x.length:
   print x[y]
   y += 1

length starts counting at 1, while indexes are zero based. Yet because we use < it works fine. Using <= would indeed cause a problem.

same with list slicing


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