Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Fix formatting, spelling
Source Link
Martin Tournoij
>>> l=['abc'l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz&']
  
# I want a string uptoup to 'def' from 'vwx', all in between
# #fromfrom 'vwx'  so -2;to 'def' just before 'abc' so -9;backwards9; backwards all so -1.
>>> l[-2:-9:-1]
 #['vwx'['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def'] 

# #ForFor the same  'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
>>> l[7:0:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']


Please do not become listless about list. 1.Wite the first element first .You can use positive or negative index for that.I am lazy so I use positive, one stroke less. (Below 7 or , -3 for the start.) 2. index of the element just before where you want to stop. Again, you can use positive or negative index for that.( Below 2 or -8 for stop.) 3.Here sign matters;of course - for backwards; value of stride you know. [stride is a 'vector' with both magnitude and direction!] (Below -1,backwards all) l=[0,1,2,3,4,5,6,7,8,9] l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1] All

  1. Write the first element first. You can use positive or negative index for that. I am lazy so I use positive, one stroke less (below 7, or -3 for the start).

  2. Index of the element just before where you want to stop. Again, you can use positive or negative index for that (below 2 or -8 for stop).

  3. Here sign matters; of course - for backwards; value of stride you know. Stride is a 'vector' with both magnitude and direction (below -1, backwards all).

     l = [0,1,2,3,4,5,6,7,8,9]
     l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1]
    

All result in [7, 6, 5, 4, 3][7, 6, 5, 4, 3].

 l=['abc', 'def','ghi','jkl','mno','pqr', 'stu','vwx','yz&']
 # I want a string upto 'def' from 'vwx',all in between
 #from 'vwx'  so -2;to 'def' just before 'abc' so -9;backwards all so -1.
 l[-2:-9:-1]
 #['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']
 #For the same  'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
 l[7:0:-1]

['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']


Please do not become listless about list. 1.Wite the first element first .You can use positive or negative index for that.I am lazy so I use positive, one stroke less. (Below 7 or , -3 for the start.) 2. index of the element just before where you want to stop. Again, you can use positive or negative index for that.( Below 2 or -8 for stop.) 3.Here sign matters;of course - for backwards; value of stride you know. [stride is a 'vector' with both magnitude and direction!] (Below -1,backwards all) l=[0,1,2,3,4,5,6,7,8,9] l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1] All result in [7, 6, 5, 4, 3]

>>> l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz&']
 
# I want a string up to 'def' from 'vwx', all in between
# from 'vwx' so -2;to 'def' just before 'abc' so -9; backwards all so -1.
>>> l[-2:-9:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def'] 

# For the same 'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
>>> l[7:0:-1]
['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']

Please do not become listless about list.

  1. Write the first element first. You can use positive or negative index for that. I am lazy so I use positive, one stroke less (below 7, or -3 for the start).

  2. Index of the element just before where you want to stop. Again, you can use positive or negative index for that (below 2 or -8 for stop).

  3. Here sign matters; of course - for backwards; value of stride you know. Stride is a 'vector' with both magnitude and direction (below -1, backwards all).

     l = [0,1,2,3,4,5,6,7,8,9]
     l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1]
    

All result in [7, 6, 5, 4, 3].

Source Link
Rathinavelu Muthaliar

 l=['abc', 'def','ghi','jkl','mno','pqr', 'stu','vwx','yz&']
 # I want a string upto 'def' from 'vwx',all in between
 #from 'vwx'  so -2;to 'def' just before 'abc' so -9;backwards all so -1.
 l[-2:-9:-1]
 #['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']
 #For the same  'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
 l[7:0:-1]

['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']


Please do not become listless about list. 1.Wite the first element first .You can use positive or negative index for that.I am lazy so I use positive, one stroke less. (Below 7 or , -3 for the start.) 2. index of the element just before where you want to stop. Again, you can use positive or negative index for that.( Below 2 or -8 for stop.) 3.Here sign matters;of course - for backwards; value of stride you know. [stride is a 'vector' with both magnitude and direction!] (Below -1,backwards all) l=[0,1,2,3,4,5,6,7,8,9] l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1] All result in [7, 6, 5, 4, 3]

lang-py