I was wondering why the following that when I have list in python , lis
Why does lis[1:3] only includes the 2nd and 3rd element of the list rather than 2nd, 3rd and 4th element?
            Asked
            
        
        
            Active
            
        
            Viewed 40 times
        
    0
            
            
         
    
    
        Kamster
        
- 127
- 7
- 
                    1Because the first index is 0, not 1. – Wondercricket Oct 09 '15 at 20:13
- 
                    2When you have a slice such as `[a:b]` it starts at element `a` then goes to element `b-1`. See this: http://stackoverflow.com/questions/509211/explain-pythons-slice-notation – shuttle87 Oct 09 '15 at 20:14
- 
                    because that's how slicing works... – Chad S. Oct 09 '15 at 20:14
- 
                    @Wondercricket sorry that was a mistake – Kamster Oct 09 '15 at 20:14
- 
                    The problem is you have no natural way to represent an empty slice if `[a:b]` included `b`. `[0:0]` is an empty slice; if the end were included what would you do? `[0:-1]`? – rlbond Oct 09 '15 at 20:18
1 Answers
0
            
            
        One reason is that it makes it easier to extract a sublist of a certain length.
For instance, a[i:i+n] will extract the sublist of length n starting at element i of the list a.
This convention also dovetails with the way range(...) and xrange(...) work.
 
    
    
        ErikR
        
- 51,541
- 9
- 73
- 124
- 
                    ok, maybe I that makes it a little clearer why they use that convetion. I guess just first glance I would expect it to go other way – Kamster Oct 09 '15 at 20:16