List Slicing


I don't understand why this code works the way it does, can someone please explain it?

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[4:6] # The last two items (index four and five)

print first
print middle
print last
print suitcase

In the variable 'middle', shouldn't the correct answer be suitcase=[2:3]? When I put in said information and tried to print it, it only printed 'passport'.

I'm just a little confused.



I think I may have figured it out, but please correct me if I am wrong.

[2:4] literally translates to "2 TO 4", meaning it is slicing every index from 2 to 4... which includes 3 but not 4.

If I were to put in [2:3], it only slices the 2nd index in that format as it goes from 2 TO 3, but doesn't actually include 3.


yes, that is correct. when you put in [2:3], it will not include 3.