Slicing - Python Sequence

AUTHOR: RAJA RAMANATHAN                                                        Sunday, October 08, 2006

Purpose:

        Slicing operation is to select a range of elements in a sequence.

Syntax:

        sequence [starting-at-index : but-less-than-index : step]
        where the parameter
             1. sequence is name of the sequence
             2. starting-at-index is the index of the first element (inclusive)
                    2.1 It is always required.
                    2.2 Default is 0.
             3. but-less-than-index:  is the index of the last element (exclusive)
                    3.1 It is always required.
                    3.2 Default is the number of elements in sequence. len(sequence).
             4. step: is the length of the step when slicing.
                  4.1 It is optional
                  4.2 Default is 1
                  4.3 Positive step means slicing from left to right (forward)
                  4.4 Negative step means slicing from right to left (backward)
                  4.5 Cannot be zero.

Let us continue with our previous example used in indexing.
                vowels     = ['a', 'e', 'i', 'o', 'u']
                +ve index   --0--1--2--3--4--
                -ve index   --5--4--3--2--1--

Forward direction

                 If the step is positive then it is slicing in forward direction. .
                             vowels[0:1] #prints ['a']
                             vowels[0:2] #prints ['a', 'e'] (element 2 is excluded)
                             vowels[1:3] #prints ['e', 'i'] (element 3 is excluded)
                             vowels[0:]   #prints all the elements
                             vowels[::2] #prints ['a', 'i', 'u']
                             vowels[0:5:4] #prints ['a', 'u']
                              #using negative indexes
                              vowels[-5:-4] #prints ['a']
                              vowels[-5:-3] #prints ['a', 'e'] (element -3 is excluded)
                              vowels[-5:-2] #prints ['a', 'e', 'i']  (element -2 is excluded)
                              vowels[-5:]  #prints all the elements
                              vowels[-5::2]  #prints['a', 'i', 'u']
                              vowels[-5::4] #prints ['a', 'u']
                Note: The starting-at-index should be less than but-less-than-index else the return will be empty.

Backward direction

                    If the step is negative then it is slicing in forward direction. .
                                vowels[4:3:-1]  #prints ['u']
                                vowels[4:2:-1]  #prints ['u', 'o'] (element 2 is excluded)
                                vowels[3:1:-1]  #prints ['o', 'i'] (element 1 is excluded)
                                vowels[::-1]      #prints ['u', 'o', 'i', 'e', 'a']
                                vowels[::-2]      # prints ['u', 'i', 'a']
                                vowels[::-4]      #prints ['u', 'a']
                                 #using negative indexes
                                 vowels[-1:-2:-1] #prints ['u']
                                 vowels[-1:-3:-1] #prints ['u', 'o']  (element -3 is excluded)
                                 vowels[-2:-4:-1]  #prints ['o', 'i'] (element -4 is excluded)
                     Note: The starting-at-index should be greater than but-less-than-index else the return will be empty

Common Mistakes

                    1. starting-at-index greater than but-less-than-index in forward direction
                                vowels[2:1] #print empty
                    2. starting-at-index less than but-less-than-index in backward direction
                                vowels[1:2:-1] #prints empty