9. Python Lists and Dictionaries | 5. List Slicing


#1

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first  = suitcase[0:1]  # 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)

it says the first slice only shows ["sunglasses"] when it should show ["sunglasses, "hat"] what is wrong?????


#2

@icewolf4256,

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]
# The first and second items (index zero and one)
first  = suitcase[0:2]
print first
# Third and fourth items (index two and three)
middle = suitcase[2:4]               
print middle
# The last two items (index four and five)
last   =    suitcase[4:6]            
print last

#3

Thank you so much!!! This really got me past a block.


#5

Up to but not including? This seems like a very convoluted way of pulling things from a list...


#6

i dont understand
The exercise says

first = suitcase[0:1] # 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)

so why you changed it to

suitcase[0:2]

middle = suitcase[2:4]

last = suitcase[4:6]

did i not understand something ?


#7

Same question here. Why index zero and one is [0:2]???


#8

Well, ok, seems like i get it.
unlimited paint works...


#9

Thank you so much!!! This really got me past a block.:smiley:


#10

cheers mate!! it helped me a lot!


#11

This is so annoying! The first parameter of a slice is the index of the first element. So why the second parameter is not the index of the last element??? That means if you want the last element to be included you have to use a non existent index number! What a stupid and confusing implementation! This makes it much more complicated and not even easier.


#13

This should help,:grin:
suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first = suitcase[0:2]
middle = suitcase[2:4]
last = suitcase[4:6]

:sleepy: so easy!!! even though I got some help.:unamused: