Order of Lists (from Lesson 5.List Slicing)



I am having trouble understanding how python labels the items in an index. This is my code:

    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)

It works, but I don't get it. (I added the print part just to verify results in PyCharm.) Given that code, this is how the lists have been assigned:

sunglasses is assigned [0]
hat is assigned [2]
passport is also assigned [2]
laptop is assigned [4]
suit is also assigned [4]
shoes is assigned [6]

I would have expected each of the items to have a consistent index value. What is going on?



what is your result, if i run your code i get this:

['sunglasses', 'hat']
['passport', 'laptop']
['suit', 'shoes']

which is the correct output, i don't see a problem with it


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