Transcript
You can slice pretty much any sequence in Python. A sequence is something that you can index from 0
to len(sequence)-1
. Lists, tuples, and strings are all examples of sequences.
Let say we have a variable fruits
that points to a list:
>>> fruits = ['watermelon', 'apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange']
>>> fruits
['watermelon', 'apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange']
We can get an item from this list by indexing it:
>>> fruits[3]
'kiwi'
Slicing basics
If we put a colon and another number inside the square brackets, we're slicing this list instead of indexing it:
>>> fruits[0:3]
['watermelon', 'apple', 'lime']
Slicing a list gives us back a new list. We're getting a list of the first three items here.
What do you think we'll get if we slice using 1
and 3
instead of 0
and 3
?
>>> fruits[1:3]
['apple', 'lime']
We only get 2 items this time instead of 3.
In slicing, the first item is the start index and the second is the stop index. We stop just before the stop index. So we're getting the items at index 1
and 2
here (we stop just before 3
).
The start index is inclusive and the stop index is exclusive (we stop just before it).
Default start/stop values
The start and stop values are both optional. The start value defaults to 0
:
>>> fruits[:3]
['watermelon', 'apple', 'lime']
The stop value defaults to the end of the list:
>>> fruits[1:]
['apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange']
The exclusivity of the stop index has a nice side effect: if you slice up to index 3 and then start slicing again from 3 onward, and add those two slices together we'll get back a cop of our original list:
>>> fruits[:3] + fruits[3:]
['watermelon', 'apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange']
This happens because the stop value is always exclusive and start values is always inclusive.
Out-of-bounds slicing is allowed
Normally if you index past the end of a list, you'll get an error:
>>> fruits[15]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
But slicing past the end of the list, doesn’t raises an error. It just stops at the end of the list:
>>> fruits[:15]
['watermelon', 'apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange']
The same applies to the beginning of the list.
The step value
You can actually specify third value when slicing: the step value.
The start index defaults to 0
, the stop index defaults to the end of the list, and the optional step value, defaults to 1
:
>>> fruits[::1]
['watermelon', 'apple', 'lime', 'kiwi', 'pear', 'lemon', 'orange']
If we change the step value to 2, we'll skip every other item:
>>> fruits[::2]
['watermelon', 'lime', 'pear', 'orange']
With 3
we'll show every third item:
>>> fruits[::3]
['watermelon', 'kiwi', 'orange']
The most common step value to see is -1
. This reverses the list:
>>> fruits[::-1]
['orange', 'lemon', 'pear', 'kiwi', 'lime', 'apple', 'watermelon']
Whenever a negative step value is given, the default meaning of start and stop change. The start value will now default to the length of the list and the stop value will default to just before the beginning of the list.
I don't usually use a step value with slicing. The only use I usually have for a step value is reversing a sequence and I tend to prefer using the built-in reversed
function for that:
>>> list(reversed(fruits))
['orange', 'lemon', 'pear', 'kiwi', 'lime', 'apple', 'watermelon']
What's the use?
The most common uses of slicing in Python are grabbing the first few items:
>>> fruits[:2]
['watermelon', 'apple']
Or last few items:
We can index from the end of list as we do from the start of the list.
>>> fruits[-2:]
['lemon', 'orange']
You also might see slicing used to get every item but the first or last items. This returns everything but the first and last items:
>>> fruits[1:-1]
['apple', 'lime', 'kiwi', 'pear', 'lemon']
So, you can use slicing to get the last few things, or the first few things, or to get everything except for that last and first items, or for reversing your sequence. Those are the most common use cases for slicing in Python.
from Planet Python
via read more
No comments:
Post a Comment