I attended attended Python Gotchas workshop by Anand B. Pillai. He showed a lot of gotchas with Python. During gotchas with lists, he showed this.
This post shows basics of slicing & some other interesting things about slicing.
It is a list with 6 elements in it. To understand slicing better, consider that list as a set of six boxes placed together. Each box has an alphabet in it.
Indexing is like dealing with the contents of box. You can check contents of any box. But You can't check contents of multiple boxes at once. You can even replace contents of the box. But You can't place 2 balls in 1 box or replace 2 balls at a time.
Slicing is like dealing with boxes itself. You can pickup first box and place it on another table. To pickup the box all You need to know is the position of beginning & ending of the box.
You can even pickup first 3 boxes or last 2 boxes or all boxes between 1 & 4. So, You can pick any set of boxes if You know beginning & ending. This positions are called start & stop positions.
The interesting thing is that You can replace multiple boxes at once. Also You can place multiple boxes where ever You like.
Till now You have picked boxes continuously. But some times You need to pickup discretely. For example You can pickup every second box. You can even pickup every third box from the end. This value is called step size. This represents the gap between Your successive pickups. The step size should be positive if You are picking boxes from the beginning to end and vice versa.
When slicing if You leave out any parameter, Python tries to figure it out automatically.
If You check source code of CPython, You will find a function called PySlice_GetIndicesEx which figures out indices to a slice for any given parameters. Here is the logical equivalent code in Python.
This function takes a Python object & optional parameters for slicing and returns start, stop, step & slice length for the requested slice.
As You know basics of slicing, You can start tweaking with it or You can write Your own custom slicer!
If You find any mistake with above function, feel free to update it.
Wiki Python: https://wiki.python.org/moin/
CPython repo: https://github.com/python/cpython
Slice on SO: http://stackoverflow.com/questions/509211/explain-pythons-slice-notation
Docs for slice: https://docs.python.org/3.4/library/functions.html#slice
Python/C API for slice: https://docs.python.org/3.4/c-api/slice.html
Thanks to Anand B. Pillai & Krace Kumar for the meetup & encouraging to checkout Python source code.
Read more articles about Python!
Also let us consider some other cases.
If we look at above cases, we can see that indexing works within the range of length of object, otherwise it throws error. On the other hand, slicing returns an empty list instead of throwing error. Also slicing intelligently figures out missing parameters.This post shows basics of slicing & some other interesting things about slicing.
Slicing Is NOT Indexing:
Wiki Python has this amazing picture which clearly distinguishes indexing and slicing.
It is a list with 6 elements in it. To understand slicing better, consider that list as a set of six boxes placed together. Each box has an alphabet in it.
Indexing is like dealing with the contents of box. You can check contents of any box. But You can't check contents of multiple boxes at once. You can even replace contents of the box. But You can't place 2 balls in 1 box or replace 2 balls at a time.
Slicing is like dealing with boxes itself. You can pickup first box and place it on another table. To pickup the box all You need to know is the position of beginning & ending of the box.
You can even pickup first 3 boxes or last 2 boxes or all boxes between 1 & 4. So, You can pick any set of boxes if You know beginning & ending. This positions are called start & stop positions.
The interesting thing is that You can replace multiple boxes at once. Also You can place multiple boxes where ever You like.
Slicing With Step:
How Python Figures Out Missing Parameters:
When slicing if You leave out any parameter, Python tries to figure it out automatically.
If You check source code of CPython, You will find a function called PySlice_GetIndicesEx which figures out indices to a slice for any given parameters. Here is the logical equivalent code in Python.
This function takes a Python object & optional parameters for slicing and returns start, stop, step & slice length for the requested slice.
This is the intelligence that is present behind slices. Since Python has inbuilt function called slice, You can pass some parameters & check how smartly it calculates missing parameters.
As You know basics of slicing, You can start tweaking with it or You can write Your own custom slicer!
If You find any mistake with above function, feel free to update it.
References:
Wiki Python: https://wiki.python.org/moin/
CPython repo: https://github.com/python/cpython
Slice on SO: http://stackoverflow.com/questions/509211/explain-pythons-slice-notation
Docs for slice: https://docs.python.org/3.4/library/functions.html#slice
Python/C API for slice: https://docs.python.org/3.4/c-api/slice.html
Thanks to Anand B. Pillai & Krace Kumar for the meetup & encouraging to checkout Python source code.
Read more articles about Python!