List Slicing


#1


https://www.codecademy.com/courses/python-beginner-en-pwmb1/1/2?curriculum_id=4f89dab3d788890003000096#

It looks like your middle slice is ["passport"] instead of ["passport", "laptop"].


I expect the output to be ["passport", "laptop"].


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)


#2

Recall that the stop index is exclusive so not in the slice. Add 1 to the last two.


#3

What do you mean by that? I'm trying to making so that the output in the middle slice is ["passport", "laptop"]

middle = suitcase[2:3] # Third and fourth items (index two and three)

Instead it came out as ["laptop"]


#4

The difference between the start index and the stop index will be the number of elements in the slice. 3 - 2 is 1. 5 - 4 is 1. Remember the stop index is not in the slice.


#7

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