Slices and More
Slices
First: What is 'Slicing'? Slicing allows you to get a substring from a string. Or a portion of a list. That 'slice' is returned.
The syntax:
sequence[start:end:step]
.
Or you can also think of it as sequence[from index: until index (not including): step]
. Remember, Python uses a 0 index. See the docs
Direction
-
If the 'start' is absent or a positive number, then the 'start' is from the left; in other words - forward. If the 'start' is a negative number, then the 'start' index begins from the end.
-
If the 'end' is absent or a positive number, then the 'end' is from the left; in other words - forward. If the 'end' is a negative number, then the 'end' index begins from the end.
-
If the 'step' is absent or a positive number, then the 'start' is from the left; in other words - forward. If the 'step' is a negative number, then the 'start' is from the right, from the end; in other words - backward. (see examples below)
Defaults
-
If there is no 'start', then it begins from the beginning - depending on the direction.
-
If there is no 'end', then it goes until the end - depending on the direction.
-
If there is no 'step', then it goes one by one forward. If there is a 'step', then it counts that number, depending on the direction.
Some examples, using a string. And a list:
mystr = "ABCD"
mylist = ["a", "b", "c", "d"]
the code | result | explanation |
---|---|---|
mystr[0] | ||
mylist[0] | ||
mystr[1:] | ||
mylist[1:] | ||
mystr[:1] | ||
mylist[:1] | ||
mystr[1:3] | ||
mylist[1:3] | ||
mystr[1:6] | ||
mylist[1:6] | ||
mystr[1::2] | ||
mylist[1::2] | ||
mystr[-1] | ||
mylist[-1] | ||
mystr[:-1] | ||
mylist[:-1] | ||
mystr[2:2] | ||
mylist[2:2] | ||
mystr[2:1] | ||
mylist[2:1] | ||
mystr[-3:-1] | ||
mylist[-3-1] | ||
mystr[::-1] | ||
mylist[::-1] | ||
mystr[:] | ||
mylist[:] | ||
mystr[2::-1] | ||
mylist[2::-1] | ||
mystr[2:1:-1] | ||
mylist[2:1:-1] | ||
del mystr[:] | ||
del mylist[:] |
A few more cool things with Slice
Sometimes, you might find it useful to separate the actual forming of the slice and the passing that slice definition to the actual sequence or list. You can do that with the built-in method slice
. The syntax is a = slice(start, end, step)
and then you can use it with the string or list - as mylist[a]
.
>>>a = slice(1, 5, 2)
>>>mylist[a]
['b', 'd'] # start at index one, end at index 5, with step 2
>>>mystr[a]
'BD'
>>>a.start
1
>>>a.stop
5
>>>a.step
2
Conditional Start/ Stop/ Step
You can use inline conditional statements* directly in the slice definition - for the 'start', 'stop' or 'step'. Note you can use None
if you don't want to include the 'start', 'stop' or 'step'.
*Inline conditional statements are one-liner if statements. [See more] (http://www.deekras.com/if-else.html)
>>>i=True
>>>mylist[1 if i else 2]
'b'
>>>mylist[0: 2 if i else 3]
['a', 'b']
>>>mylist[::-1 if i else 1]
['d', 'c', 'b', 'a']
>>>i=2
>>>mystr[3: None if i <0 else i: -1]
'D'
Assignment
You can assign specified index with a new value. It deletes the elements in those indexes and inserts new values. - If no new values are provided, it just deletes the elements in those indexes.
- If there are more elements to be assigned than those that are deleted, then those 'extra' elements are inserted right after those that did have a place to be inserted.
It actually changes the list; and does not return anything. (You'll have to call the list to see the actual change.) Note: Assignment does not work with strings.
the code | result | explanation |
---|---|---|
mylist[0] = "1" | ||
mylist[0] = "1", "2" | ||
mylist[0:2] = "1" | ||
mylist[0:2] = "1", "2" | ||
mylist[0:1] = "12" | ||
mylist[0:1] = "12", | ||
mylist[1:2] = [] | ||
mylist[:] = "1", "2", "3" | ||
mylist[0:2] = ["1"] | ||
mylist[0:2] = ["1","2", "3"] | ||
mystr[0] = "1" |
islice
islice
is from the itertools library. It creates an iterator from the slice. This saves memory since the data is not produced from the iterator until it is needed. See the docs
>>>from itertools import islice, count
>>>islice(mylist, 0, 2)
<itertools.islice object at 0x7f00bf0937e0>
>>>list(islice(mylist, 0, 2))
['a', 'b']
>>>islice(mystr, 0, 2)
<itertools.islice object at 0x7f00bf0937e0>
>>>list(islice(mystr, 0, 2))
['A', 'B']
>>>for i in islice(count(), 0, 100, 10):
print i,
0 10 20 30 40 50 60 70 80 90