List Slicing, section 5 of Lists and Dictionaries


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

My list slice should return both items we are required to return. The challenge calls for us to return list indexes 2 and 3 of list 'suitcase' meaning your code is: suitcase[2:3]

However, code academy is only returning one item for this slice in the exercise. I have tried just index 2, and it shows the correct item for that index, then just index 3 and again it shows the item it should be showing for that index. However, as soon as I use the slice to show the items at list indexes 2 AND 3, it claims I am only returning ONE item and not two.

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)


When using list slicing, it's important to remember this: You slice up to but not including the index-value after the colon.
This means that if you want to get the 2nd and 3rd values, you would do my_var[1:3].

You will need to change your code to suitcase[2:4].


Just tried turning the 'middle' variable into a tuple instead of a slice and indeed it returns both items as it should correctly but the code academy program still recognizes it as incorrect. So there isn't anything wrong with the list or it's indexes, something wrong with the program interpreting the data passed to it. Anyone had any luck repeating/solving this same bug/issue?

middle = (suitcase[2],suitcase[3])

which returns the error message:
Oops, try again. It looks like your middle slice is ("passport", "laptop") instead of ["passport", "laptop"].


Sorry, that is incorrect. You're declaring the specific indexes you wish to return. It's not like a range which end one step before your final value.


Thanks for trying to help!


Sorry for pushing, but it's in the learning section of that task:

Try running the code I provided, and see if it works.


No problem, I understand you're quoting the material. The material is incorrect however. Try it for yourself. A slice takes the indexes you specify, a range e.g. for n in range (1,3,1) will stop before 3, but a slice does not work like a range.

This is clearly evidenced by the fact that this exercise passes my results for the variables before and after middle that call for similar list slices, it also literally tells you to insert list index 2 and 3 into your slice.


This is the code I used to pass that course. It works, and it works based on what I told you.


Still hasn't worked for me :(. You are correct about slices, but for this exercise it does not work. I've opened up the IDLE editor and tried it for myself with the different slice lengths and indexes. I still receive the same error message from the code academy editor however :(.


The SCT is expecting a list, not a tuple.

n = [1, 2, 3, 4, 5, 6, 7]

To slice the first item only,


The second item, only,


The end index is exclusive.


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