Python for Beginners [26 of 44] Demo: Collections

Play Python for Beginners [26 of 44] Demo: Collections

The Discussion

  • User profile image
    anumishra
    my question here .. why does a list does not work , if i create it from existing list .. ( but not starting from index 0 position) .. example -

    state1= {'name' : 'IL', 'capital' : 'Springfield'}
    state2={'name' : 'MN', 'capital' : 'St.paul'}
    states = [state1,state2]

    states.append({'name' : 'WI', 'capital' : 'Milwaukee'})

    # this works
    myState = states[0:2]
    print()
    print(myState)

    #this does not, why?
    neighbourStates = states[1:2]
    print()
    print(neighbourStates)
  • User profile image
    WadeTsaiMSN
    @anumishra:
    The 2nd parameter of [0:2] is not "how many items", it's the index of item after the last item.
    So if you use states[1:2], it will only return states[1].
    (Another example, if you use states[3:5], it will return states[3] and states[4].)
  • User profile image
    geektrainer
    @WadeTsaiMSN:

    I'm very sorry for the confusion here! Sometimes I move too fast in my efforts to break things down and make mistakes. I'm going to work to get back into the studio as soon as I can to update this video.

Add Your 2 Cents