Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Rollback to Revision 7. The changes introduced in Revision 8 should either be integrated more succictly into this answer (maintaining the valuable summary tables!) or moved to a new answer.
Source Link
Mateen Ulhaq

A. Preprocessing

  1. Data
>>> x = [1,x[:] 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> l = len(x) 
# l = 10
  1. Positive indexing
# x[0] = 1, x[1] = 2, x[2] = 3, x[3] = 4[x[0], x[4] = 5, x[5] = 6
# x[6] = 7x[1], x[7] = 8, x[8] = 9, x[9] = 10
# x[0] ... x[l-1]
  1. Negative indexing
# x[-1] = 10, x[-2] = 9, x[-3] = 8, x[-4] = 7, x[-5] = 6, x[-6] = 5
# x[-7] = 4, x[-8] = 3, x[-9] = 2, x[-10] = 1
# x[-1] ... x[-l]
  1. Deriving one from another
p = positive index
n = negative index
p - n = l
p = l + n
n = -l + p
p =[0, ..., l-1]
n =[-l, ..., -1]]

B. Positive stride

  1. General
>>> x[low:high:stride]
# low to high, element at index low included, 
# then next is obtained by adding the step stride to the previous element
# element at index high is excluded
# if the last jump falls to the index high-1, element at index high-1 is included
# if the last jump does not fall to the index high-1, element at index high-1 is excluded
# [x[low], x[low+stride], ..., x[last]], last <= high-1
# low < high, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if low >=] high, ==> []
  1. Stride is 1

a. General

>>> x[low:high:1]
>>> x[low:high]        
 # [x[low], x[low+1],      ..., x[highx[-1]]
# low to high, element at index low included, element at index high excluded

b. Full bounds

>>> x[0:l:1]
>>> x[0:l]
>>> x[:] 
>>> x[::1] ]
>>> x[::]high] 
# 0 to l, element at index 0 included, element at index l excluded
# [x[0] ... x[l-1]]
# [x[0],   x[1],          ..., x[-1]]
# [x[-l] ... x[lx[high-1]]

c. Lower bound restricted, upper bound is maximum

>>> x[low:]
>>> x[low::]
 # low to l, element at index low included, element at index l excluded
 # [x[low],high] x[low+1],      ..., x[l-1]]
 # [x[low], x[low+1],      ..., x[x[high-1]]

d. Upper bound restricted, lower bound is minimum

>>> x[:high]:stride] 
# 0 to high, element at index 0 included, element at index high excluded
# [x[0],   x[1]x[stride],          ..., x[high-1]]
# [x[-l],   x[-l+1],       1]   ..., x[high-1]]]
  1. Stride is more than 1, stride >= 1

a. Lower bound restricted, upper bound is maximum

>>> x[low::stride]  
# x[low:l:stride]  
  # [x[low], x[low+stride], ..., x[last]],x[-1] last <= l-1 ]

b. Upper bound restricted, lower bound is minimum

>>> x[:high:stride]
# x[0:high:stride]  
   # [x[0],   x[0+stride]x[stride],     ..., x[last]], last <= highx[high-11]]

C. Negative stride

  1. General
>>> x[highx[low:lowhigh:-stride]
# high to low, element at index high included, 
# then next is obtained by substracting the step stride to the previous element
# element at index low is excluded
# if the last jump falls to the index low+1, element at index low+1 is included
# if the last jump does not fall to the index low+1, element at index low+1 is excluded
# [x[high][x[low], x[high-stride]x[low+stride], ..., x[last]], last >= low+1
# high > low, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if high <= low, ==> []x[high-1]]
  1. Stride is 1

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1. General

>>> x[high:low:-1]      
# [x[high], x[high-1],      ..., x[last]], last >= low+1
# high to low, element at index high included, element at index low excluded

b. Full boundsIf stride is negative, the ordering is changed a bit since we're counting down:

>>> x[l-1:-l-1:-1]
>>> x[-1:-l-1:-1]
>>> x[::-1] 
# l-1 to -l-1, element at index l-1 included, element at index -l-1 excluded
# index -l-1 comes afterstride] the first element which has index 0, or -l
# -l[x[-1001], -l-99, ..., -lx[-1, -l-0, -l+1, ...stride], -1
# [x[l-1] ... x[0]
# [x[-1],   x[-1-1],       x[0]   ..., x[-l]]]

c. Upper bound restricted, lower bound is minimum

>>> x[high::-1]  
 # high to -l-1, element at index high included, element atstride] index -l-1 excluded
 # [x[high], x[high-1]stride],      ..., x[-l]]
 # [x[high], x[high-1],   x[0]   ..., x[0]]]

c. Lower bound restricted, lower bound is minimum

>>> x[:low:-1] 
# -1 to low, element at index -1stride] included, element at index low excluded
# [x[-1],   x[-1-1]stride],          ..., x[low+1]]
  1. Stride is more than 1, stride >= 1

a. Upper bound restricted, lower bound is minimum

>>> x[high::stride]  
# x[highlow:-l-1:stride]  
 # [x[high], x[high-stride], ..., x[last]], last <= 0, or -l

b. Lower bound restricted, upper bound is maximum

>>> x[:low:stride]
# x[-1:low:stride]  
# [x[-1],   x[-1-stride],     ..., x[last]], last >= low+1x[low+1]]

A. Preprocessing

  1. Data
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> l = len(x) 
# l = 10
  1. Positive indexing
# x[0] = 1, x[1] = 2, x[2] = 3, x[3] = 4, x[4] = 5, x[5] = 6
# x[6] = 7, x[7] = 8, x[8] = 9, x[9] = 10
# x[0] ... x[l-1]
  1. Negative indexing
# x[-1] = 10, x[-2] = 9, x[-3] = 8, x[-4] = 7, x[-5] = 6, x[-6] = 5
# x[-7] = 4, x[-8] = 3, x[-9] = 2, x[-10] = 1
# x[-1] ... x[-l]
  1. Deriving one from another
p = positive index
n = negative index
p - n = l
p = l + n
n = -l + p
p =[0, ..., l-1]
n =[-l, ..., -1]

B. Positive stride

  1. General
>>> x[low:high:stride]
# low to high, element at index low included, 
# then next is obtained by adding the step stride to the previous element
# element at index high is excluded
# if the last jump falls to the index high-1, element at index high-1 is included
# if the last jump does not fall to the index high-1, element at index high-1 is excluded
# [x[low], x[low+stride], ..., x[last]], last <= high-1
# low < high, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if low >= high, ==> []
  1. Stride is 1

a. General

>>> x[low:high:1]
>>> x[low:high]        
 # [x[low], x[low+1],      ..., x[high-1]]
# low to high, element at index low included, element at index high excluded

b. Full bounds

>>> x[0:l:1]
>>> x[0:l]
>>> x[:] 
>>> x[::1] 
>>> x[::] 
# 0 to l, element at index 0 included, element at index l excluded
# [x[0] ... x[l-1]]
# [x[0],   x[1],          ..., x[-1]]
# [x[-l] ... x[l-1]]

c. Lower bound restricted, upper bound is maximum

>>> x[low:]
>>> x[low::]
 # low to l, element at index low included, element at index l excluded
 # [x[low], x[low+1],      ..., x[l-1]]
 # [x[low], x[low+1],      ..., x[-1]]

d. Upper bound restricted, lower bound is minimum

>>> x[:high] 
# 0 to high, element at index 0 included, element at index high excluded
# [x[0],   x[1],          ..., x[high-1]]
# [x[-l],   x[-l+1],          ..., x[high-1]]
  1. Stride is more than 1, stride >= 1

a. Lower bound restricted, upper bound is maximum

>>> x[low::stride]  
# x[low:l:stride]  
 # [x[low], x[low+stride], ..., x[last]], last <= l-1

b. Upper bound restricted, lower bound is minimum

>>> x[:high:stride]
# x[0:high:stride]  
 # [x[0],   x[0+stride],     ..., x[last]], last <= high-1

C. Negative stride

  1. General
>>> x[high:low:-stride]
# high to low, element at index high included, 
# then next is obtained by substracting the step stride to the previous element
# element at index low is excluded
# if the last jump falls to the index low+1, element at index low+1 is included
# if the last jump does not fall to the index low+1, element at index low+1 is excluded
# [x[high], x[high-stride], ..., x[last]], last >= low+1
# high > low, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if high <= low, ==> []
  1. Stride is 1

a. General

>>> x[high:low:-1]      
# [x[high], x[high-1],      ..., x[last]], last >= low+1
# high to low, element at index high included, element at index low excluded

b. Full bounds

>>> x[l-1:-l-1:-1]
>>> x[-1:-l-1:-1]
>>> x[::-1] 
# l-1 to -l-1, element at index l-1 included, element at index -l-1 excluded
# index -l-1 comes after the first element which has index 0, or -l
# -l-100, -l-99, ..., -l-1, -l-0, -l+1, ..., -1
# [x[l-1] ... x[0]
# [x[-1],   x[-1-1],          ..., x[-l]]

c. Upper bound restricted, lower bound is minimum

>>> x[high::-1]  
 # high to -l-1, element at index high included, element at index -l-1 excluded
 # [x[high], x[high-1],      ..., x[-l]]
 # [x[high], x[high-1],      ..., x[0]]

c. Lower bound restricted, lower bound is minimum

>>> x[:low:-1] 
# -1 to low, element at index -1 included, element at index low excluded
# [x[-1],   x[-1-1],          ..., x[low+1]]
  1. Stride is more than 1, stride >= 1

a. Upper bound restricted, lower bound is minimum

>>> x[high::stride]  
# x[high:-l-1:stride]  
 # [x[high], x[high-stride], ..., x[last]], last <= 0, or -l

b. Lower bound restricted, upper bound is maximum

>>> x[:low:stride]
# x[-1:low:stride]  
# [x[-1],   x[-1-stride],     ..., x[last]], last >= low+1
>>> x[:]                # [x[0],   x[1],          ..., x[-1]    ]
>>> x[low:]             # [x[low], x[low+1],      ..., x[-1]    ]
>>> x[:high]            # [x[0],   x[1],          ..., x[high-1]]
>>> x[low:high]         # [x[low], x[low+1],      ..., x[high-1]]
>>> x[::stride]         # [x[0],   x[stride],     ..., x[-1]    ]
>>> x[low::stride]      # [x[low], x[low+stride], ..., x[-1]    ]
>>> x[:high:stride]     # [x[0],   x[stride],     ..., x[high-1]]
>>> x[low:high:stride]  # [x[low], x[low+stride], ..., x[high-1]]

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1.

If stride is negative, the ordering is changed a bit since we're counting down:

>>> x[::-stride]        # [x[-1],   x[-1-stride],   ..., x[0]    ]
>>> x[high::-stride]    # [x[high], x[high-stride], ..., x[0]    ]
>>> x[:low:-stride]     # [x[-1],   x[-1-stride],   ..., x[low+1]]
>>> x[high:low:-stride] # [x[high], x[high-stride], ..., x[low+1]]
Result elaborated in depth
Source Link
Adam

A. Preprocessing

  1. Data
>>> x[:]x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> l = len(x) 
# l = 10
  1. Positive indexing
# [x[0],x[0] = 1, x[1] = 2, x[2] = 3, x[3] = 4, x[4] = 5, x[5] = 6
# x[6] = 7, x[7] = 8, x[8] = 9, x[9] = 10
# x[0] ... x[l-1]
  1. Negative indexing
# x[-1] = 10, x[-2] = 9, x[-3] = 8, x[-4] = 7, x[-5] = 6, x[-6] = 5
# x[-7] = 4, x[-8] = 3, x[-9] = 2, x[-10] = 1
# x[-1] ... x[-l]
  1. Deriving one from another
p = positive index
n ]= negative index
p - n = l
p = l + n
n = -l + p
p =[0, ..., l-1]
n =[-l, ..., -1]

B. Positive stride

  1. General
>>> x[low:]high:stride]
# low to high, element at index low included, 
# then next is obtained by adding the step stride to the previous element
# element at index high is excluded
# if the last jump falls to the index high-1, element at index high-1 is included
# if the last jump does not fall to the index high-1, element at index high-1 is excluded
# [x[low], x[low+stride], ..., x[last]], last <= high-1
# low < high, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if low >= high, ==> []
  1. Stride is 1

a. General

>>> x[low:high:1]
>>> x[low:high]         
# [x[low], x[low+1],      ..., x[x[high-1]1]]
# low to high, element at index low included, element at index high excluded

b. Full bounds

>>> x[0:l:1]
>>> x[0:l]
>>> x[:] 
>>> x[:high]:1] 
>>> x[::] 
# 0 to l, element at index 0 included, element at index l excluded
# [x[0] ... x[l-1]]
# [x[0],   x[1],          ..., x[highx[-1]]
# [x[-l] ... x[l-1]]

c. Lower bound restricted, upper bound is maximum

>>> x[low:]
>>> x[low:high]:]
 # low to l, element at index low included, element at index l excluded
 # [x[low], x[low+1],      ..., x[highx[l-1]]
 # [x[low], x[low+1],      ..., x[-1]]

d. Upper bound restricted, lower bound is minimum

>>> x[::stride]high] 
# 0 to high, element at index 0 included, element at index high excluded
# [x[0],   x[stride]x[1],          ..., x[high-1]]
# [x[-l],   x[-1]l+1],    ]      ..., x[high-1]]
  1. Stride is more than 1, stride >= 1

a. Lower bound restricted, upper bound is maximum

>>> x[low::stride]   
# x[low:l:stride]   
# [x[low], x[low+stride], ..., x[-1] x[last]], last <= ]l-1

b. Upper bound restricted, lower bound is minimum

>>> x[:high:stride]  
# x[0:high:stride]   
# [x[0],   x[stride]x[0+stride],     ..., x[highx[last]], last <= high-1]]1

C. Negative stride

  1. General
>>> x[lowx[high:highlow:-stride]
# high to low, element at index high included,  
# [x[low]then next is obtained by substracting the step stride to the previous element
# element at index low is excluded
# if the last jump falls to the index low+1, x[low+stride]element at index low+1 is included
# if the last jump does not fall to the index low+1, element at index low+1 is excluded
# [x[high], x[high-stride], ..., x[high-1]]x[last]], last >= low+1
# high > low, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if high <= low, ==> []
  1. Stride is 1

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1. General

>>> x[high:low:-1]      
# [x[high], x[high-1],      ..., x[last]], last >= low+1
# high to low, element at index high included, element at index low excluded

If stride is negative, the ordering is changed a bit since we're counting down:b. Full bounds

>>> x[l-1:-l-1:-1]
>>> x[-1:-l-1:-stride]1]
>>> x[::-1] 
# l-1 to -l-1, element at #index [x[l-1]1 included, element at x[index -l-1 excluded
# index -stride]l-1 comes after the first element which has index 0, or -l
# -l-100, -l-99, ..., -l-1, -l-0, -l+1, ..., -1
# [x[l-1] ... x[0]
# [x[-1],   x[-1-1], ]         ..., x[-l]]

c. Upper bound restricted, lower bound is minimum

>>> x[high::-stride]1]  
 # high to -l-1, element at index high included, element at index -l-1 excluded
 # [x[high], x[high-stride]1],      ..., x[0]x[-l]]
 # [x[high], x[high-1], ]     ..., x[0]]

c. Lower bound restricted, lower bound is minimum

>>> x[:low:-stride]1] 
# -1 to low, element at index -1 included, element at index low excluded
# [x[-1],   x[-1-stride]1],          ..., x[low+1]]
  1. Stride is more than 1, stride >= 1

a. Upper bound restricted, lower bound is minimum

>>> x[high:low:stride]  
# x[high:-l-1:stride]   
# [x[high], x[high-stride], ..., x[low+1]]x[last]], last <= 0, or -l

b. Lower bound restricted, upper bound is maximum

>>> x[:low:stride]
# x[-1:low:stride]  
# [x[-1],   x[-1-stride],     ..., x[last]], last >= low+1
>>> x[:]                # [x[0],   x[1],          ..., x[-1]    ]
>>> x[low:]             # [x[low], x[low+1],      ..., x[-1]    ]
>>> x[:high]            # [x[0],   x[1],          ..., x[high-1]]
>>> x[low:high]         # [x[low], x[low+1],      ..., x[high-1]]
>>> x[::stride]         # [x[0],   x[stride],     ..., x[-1]    ]
>>> x[low::stride]      # [x[low], x[low+stride], ..., x[-1]    ]
>>> x[:high:stride]     # [x[0],   x[stride],     ..., x[high-1]]
>>> x[low:high:stride]  # [x[low], x[low+stride], ..., x[high-1]]

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1.

If stride is negative, the ordering is changed a bit since we're counting down:

>>> x[::-stride]        # [x[-1],   x[-1-stride],   ..., x[0]    ]
>>> x[high::-stride]    # [x[high], x[high-stride], ..., x[0]    ]
>>> x[:low:-stride]     # [x[-1],   x[-1-stride],   ..., x[low+1]]
>>> x[high:low:-stride] # [x[high], x[high-stride], ..., x[low+1]]

A. Preprocessing

  1. Data
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> l = len(x) 
# l = 10
  1. Positive indexing
# x[0] = 1, x[1] = 2, x[2] = 3, x[3] = 4, x[4] = 5, x[5] = 6
# x[6] = 7, x[7] = 8, x[8] = 9, x[9] = 10
# x[0] ... x[l-1]
  1. Negative indexing
# x[-1] = 10, x[-2] = 9, x[-3] = 8, x[-4] = 7, x[-5] = 6, x[-6] = 5
# x[-7] = 4, x[-8] = 3, x[-9] = 2, x[-10] = 1
# x[-1] ... x[-l]
  1. Deriving one from another
p = positive index
n = negative index
p - n = l
p = l + n
n = -l + p
p =[0, ..., l-1]
n =[-l, ..., -1]

B. Positive stride

  1. General
>>> x[low:high:stride]
# low to high, element at index low included, 
# then next is obtained by adding the step stride to the previous element
# element at index high is excluded
# if the last jump falls to the index high-1, element at index high-1 is included
# if the last jump does not fall to the index high-1, element at index high-1 is excluded
# [x[low], x[low+stride], ..., x[last]], last <= high-1
# low < high, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if low >= high, ==> []
  1. Stride is 1

a. General

>>> x[low:high:1]
>>> x[low:high]         
# [x[low], x[low+1],      ..., x[high-1]]
# low to high, element at index low included, element at index high excluded

b. Full bounds

>>> x[0:l:1]
>>> x[0:l]
>>> x[:] 
>>> x[::1] 
>>> x[::] 
# 0 to l, element at index 0 included, element at index l excluded
# [x[0] ... x[l-1]]
# [x[0],   x[1],          ..., x[-1]]
# [x[-l] ... x[l-1]]

c. Lower bound restricted, upper bound is maximum

>>> x[low:]
>>> x[low::]
 # low to l, element at index low included, element at index l excluded
 # [x[low], x[low+1],      ..., x[l-1]]
 # [x[low], x[low+1],      ..., x[-1]]

d. Upper bound restricted, lower bound is minimum

>>> x[:high] 
# 0 to high, element at index 0 included, element at index high excluded
# [x[0],   x[1],          ..., x[high-1]]
# [x[-l],   x[-l+1],          ..., x[high-1]]
  1. Stride is more than 1, stride >= 1

a. Lower bound restricted, upper bound is maximum

>>> x[low::stride]  
# x[low:l:stride]   
# [x[low], x[low+stride], ..., x[last]], last <= l-1

b. Upper bound restricted, lower bound is minimum

>>> x[:high:stride]
# x[0:high:stride]   
# [x[0],   x[0+stride],     ..., x[last]], last <= high-1

C. Negative stride

  1. General
>>> x[high:low:-stride]
# high to low, element at index high included,  
# then next is obtained by substracting the step stride to the previous element
# element at index low is excluded
# if the last jump falls to the index low+1, element at index low+1 is included
# if the last jump does not fall to the index low+1, element at index low+1 is excluded
# [x[high], x[high-stride], ..., x[last]], last >= low+1
# high > low, if one of the index is negative add the length l to it 
# then comparing it with positive indices
# if high <= low, ==> []
  1. Stride is 1

a. General

>>> x[high:low:-1]      
# [x[high], x[high-1],      ..., x[last]], last >= low+1
# high to low, element at index high included, element at index low excluded

b. Full bounds

>>> x[l-1:-l-1:-1]
>>> x[-1:-l-1:-1]
>>> x[::-1] 
# l-1 to -l-1, element at index l-1 included, element at index -l-1 excluded
# index -l-1 comes after the first element which has index 0, or -l
# -l-100, -l-99, ..., -l-1, -l-0, -l+1, ..., -1
# [x[l-1] ... x[0]
# [x[-1],   x[-1-1],          ..., x[-l]]

c. Upper bound restricted, lower bound is minimum

>>> x[high::-1]  
 # high to -l-1, element at index high included, element at index -l-1 excluded
 # [x[high], x[high-1],      ..., x[-l]]
 # [x[high], x[high-1],      ..., x[0]]

c. Lower bound restricted, lower bound is minimum

>>> x[:low:-1] 
# -1 to low, element at index -1 included, element at index low excluded
# [x[-1],   x[-1-1],          ..., x[low+1]]
  1. Stride is more than 1, stride >= 1

a. Upper bound restricted, lower bound is minimum

>>> x[high::stride]  
# x[high:-l-1:stride]   
# [x[high], x[high-stride], ..., x[last]], last <= 0, or -l

b. Lower bound restricted, upper bound is maximum

>>> x[:low:stride]
# x[-1:low:stride]  
# [x[-1],   x[-1-stride],     ..., x[last]], last >= low+1
Shorter variable name to emphasize structure/relationships further. :%s/seq/x/g
Source Link
Mateen Ulhaq

Enumerating the possibilities allowed by the grammar for the sequence x:

>>> seq[x[:]                # [seq[0][x[0],   seq[1]x[1],          ..., seq[x[-1]    ]
>>> seq[lowx[low:]             # [seq[low][x[low], seq[low+1]x[low+1],      ..., seq[x[-1]    ]
>>> seq[x[:high]            # [seq[0][x[0],   seq[1]x[1],          ..., seq[highx[high-1]]
>>> seq[lowx[low:high]         # [seq[low][x[low], seq[low+1]x[low+1],      ..., seq[highx[high-1]]
>>> seq[x[::stride]         # [seq[0][x[0],   seq[stride]x[stride],     ..., seq[x[-1]    ]
>>> seq[lowx[low::stride]      # [seq[low][x[low], seq[low+stride]x[low+stride], ..., seq[x[-1]    ]
>>> seq[x[:high:stride]     # [seq[0][x[0],   seq[stride]x[stride],     ..., seq[highx[high-1]]
>>> seq[lowx[low:high:stride]  # [seq[low][x[low], seq[low+stride]x[low+stride], ..., seq[highx[high-1]]

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1.

If stride is negative, the ordering is changed a bit since we're counting down:

>>> seq[x[::-stride]        # [seq[[x[-1],   seq[x[-1-stride],   ..., seq[0]x[0]    ]
>>> seq[highx[high::-stride]    # [seq[high][x[high], seq[highx[high-stride], ..., seq[0]x[0]    ]
>>> seq[x[:low:-stride]     # [seq[[x[-1],   seq[x[-1-stride],   ..., seq[low+1]]x[low+1]]
>>> seq[highx[high:low:-stride] # [seq[high][x[high], seq[highx[high-stride], ..., seq[low+1]]x[low+1]]

Extended slicing (with commas and ellipses) are mostly used only by special data structures (like NumPy); the basic sequences don't support them.

>>> class slicee:
...     def __getitem__(self, item):
...         return repr(item)
...
>>> slicee()[0, 1:2, ::5, ...]
'(0, slice(1, 2, None), slice(None, None, 5), Ellipsis)'

Enumerating the possibilities allowed by the grammar:

>>> seq[:]                # [seq[0],   seq[1],          ..., seq[-1]    ]
>>> seq[low:]             # [seq[low], seq[low+1],      ..., seq[-1]    ]
>>> seq[:high]            # [seq[0],   seq[1],          ..., seq[high-1]]
>>> seq[low:high]         # [seq[low], seq[low+1],      ..., seq[high-1]]
>>> seq[::stride]         # [seq[0],   seq[stride],     ..., seq[-1]    ]
>>> seq[low::stride]      # [seq[low], seq[low+stride], ..., seq[-1]    ]
>>> seq[:high:stride]     # [seq[0],   seq[stride],     ..., seq[high-1]]
>>> seq[low:high:stride]  # [seq[low], seq[low+stride], ..., seq[high-1]]

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1.

If stride is negative, the ordering is changed a bit since we're counting down:

>>> seq[::-stride]        # [seq[-1],   seq[-1-stride],   ..., seq[0]    ]
>>> seq[high::-stride]    # [seq[high], seq[high-stride], ..., seq[0]    ]
>>> seq[:low:-stride]     # [seq[-1],   seq[-1-stride],   ..., seq[low+1]]
>>> seq[high:low:-stride] # [seq[high], seq[high-stride], ..., seq[low+1]]

Extended slicing (with commas and ellipses) are mostly used only by special data structures (like NumPy); the basic sequences don't support them.

>>> class slicee:
...     def __getitem__(self, item):
...         return repr(item)
...
>>> slicee()[0, 1:2, ::5, ...]
'(0, slice(1, 2, None), slice(None, None, 5), Ellipsis)'

Enumerating the possibilities allowed by the grammar for the sequence x:

>>> x[:]                # [x[0],   x[1],          ..., x[-1]    ]
>>> x[low:]             # [x[low], x[low+1],      ..., x[-1]    ]
>>> x[:high]            # [x[0],   x[1],          ..., x[high-1]]
>>> x[low:high]         # [x[low], x[low+1],      ..., x[high-1]]
>>> x[::stride]         # [x[0],   x[stride],     ..., x[-1]    ]
>>> x[low::stride]      # [x[low], x[low+stride], ..., x[-1]    ]
>>> x[:high:stride]     # [x[0],   x[stride],     ..., x[high-1]]
>>> x[low:high:stride]  # [x[low], x[low+stride], ..., x[high-1]]

Of course, if (high-low)%stride != 0, then the end point will be a little lower than high-1.

If stride is negative, the ordering is changed a bit since we're counting down:

>>> x[::-stride]        # [x[-1],   x[-1-stride],   ..., x[0]    ]
>>> x[high::-stride]    # [x[high], x[high-stride], ..., x[0]    ]
>>> x[:low:-stride]     # [x[-1],   x[-1-stride],   ..., x[low+1]]
>>> x[high:low:-stride] # [x[high], x[high-stride], ..., x[low+1]]

Extended slicing (with commas and ellipses) are mostly used only by special data structures (like NumPy); the basic sequences don't support them.

>>> class slicee:
...     def __getitem__(self, item):
...         return repr(item)
...
>>> slicee()[0, 1:2, ::5, ...]
'(0, slice(1, 2, None), slice(None, None, 5), Ellipsis)'
"backticks are deprecated in favour of repr" in Python 3
Source Link
Georgy
Loading
Active reading [<http://en.wikipedia.org/wiki/NumPy>].
Source Link
Peter Mortensen
Loading
Accounted for negative stride
Source Link
ephemient
Loading
Jeremy McGibbon
Loading
deleted 1 characters in body
Source Link
ephemient
Loading
Source Link
ephemient
Loading
lang-py