[Python](EN) Python slicing basic and examples

List slicing examples in Python


Environment and Prerequisite

  • Python(3.X)


What is Python slicing?

  • Slicing or Slice: refers the method and notation of importing objects by specifying a range of sequential objects (such as lists, tuples, strings).
  • Slicing makes new objects. Easily say, copy some portion of objects.


Basic Usage and Form

Basic Form

  • Consider there is sequential objects data structure(example: list, tuple, string) which name is a. Its basic form of slicing is like below.

a[start : end : step]

  • Each start, end, step can have both positive or negative number.
  • start: Start point of slicing.
  • end: End point of slicing. Note that it does not include end point!
  • step: Also known as stride, determine moving steps of import and direction. It is an option. Check below examples for your understanding.


Position of Indices Values

  • Like explained above, it can have Positive or Negative number.
  • Positive Number: It starts number 0 from the front of data structure and goes forward by increasing the index.
  • Negative Number: It starts number -1 from back of data structure and goes backward by decreasing the index.
  • Below shows example of indices values in list ['a', 'b', 'c', 'd', 'e'].
a = ['a', 'b', 'c', 'd', 'e']

// Index References
-------------------------------
|  a  |  b  |  c  |  d  |  e  |
-------------------------------
|  0  |  1  |  2  |  3  |  4  |          // In case of positive number
-------------------------------
| -5  | -4  | -3  | -2  | -1  |          // In case of negative number
-------------------------------


Example

  • All examples are base on list a = ['a', 'b', 'c', 'd', 'e'].


Import from specific start point to end

  • a[ start : ]

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ 1 :  ]
['b', 'c', 'd', 'e']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ -3 :  ]
['c', 'd', 'e']


Import from start point to specific end point

  • a[ : end ]

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[  : 2 ]
['a', 'b']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[  : -1 ]
['a', 'b', 'c', 'd']


Import from a specific point to a specific point

  • a[ start : end ]

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ 2 : 4 ]
['c', 'd']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ -4 : -2 ]
['b', 'c']


>>> a = ['a', 'b', 'c', 'd', 'e']
# get values of index from 1 to 3 in reverse
>>> a[ 3 : 0 : -1]
['d', 'c', 'b']


Example of step

  • a[ start : end : step ]
  • If step is positive: Get value per size of ‘step’ while moving to right.
  • If step is negative: Get value per size of ‘step’ while moving to left.

>>> a = ['a', 'b', 'c', 'd', 'e']
# get value while moving 2 steps.
>>> a[ : : 2 ]
['a', 'c', 'e']


>>> a = ['a', 'b', 'c', 'd', 'e']
# get value while moving 3 steps.
>>> a[ -5 : : 3 ]
['a', 'd']


>>> a = ['a', 'b', 'c', 'd', 'e']
# get all in reverse.
>>> a[ : : -1 ]
['e', 'd', 'c', 'b', 'a']


>>> a = ['a', 'b', 'c', 'd', 'e']
>>> a[ 3 : : -1 ]
['d', 'c', 'b', 'a']


Reference