Slicing
Slicing extracts a subset of elements from a sequence. In python, elements are sliced based on their index in the stream. As a sequence(List, tuple, and string) is an ordered stream of data, it can be indexed and sliced while a collection(Set and dictionary) is an unordered stream of data, thus they can’t be indexed and sliced. Slicing is one of the most powerful and useful feature in python. It helps in abstracting a certain part of sequences that needs to be focused rather than the whole sequence. This aids readability.
Slicing is just like indexing, but rather than returning an element of a sequence, it returns a part of a sequence. It mainly uses three parameters enclosed in square brackets []:
Sequence[ start : stop : step ]
- start: Starting index where the slicing of sequence starts. By default value is 0.
- stop: Ending index where the slicing of sequence stops+1. By default value is len(Sequence).
- step: Integer value which determines the increment between each index for slicing. By default value is 1.
Usage of start, stop, and step parameters is optional. Python uses negative indexes to represent positions from the end of the sequence. A reverse subsequence is obtained by using a negative step and by switching the start and end indices.
Examples-:
list = [1,2,3,4,5,6,7,8,9] tuple = ('a','b','c','d','e','f','g','h','i') string = "Python For You" #slicing with only start parameter print(list[1:]) #Output: [2, 3, 4, 5, 6, 7, 8, 9] print(tuple[5:]) #Output: ('f', 'g', 'h', 'i') print(string[-3:]) #Output: You #the resulting sequences stoped at the end of the original sequences #slicing with only end parameter print(list[:7]) #Output: [1, 2, 3, 4, 5, 6, 7] print(tuple[:-5]) #Output: ('a', 'b', 'c', 'd') print(string[:10]) #Output: Python For #the resulting sequences started from the beginning of the original sequences #slicing with start and end parameters print(list[1:-1]) #Output: [2, 3, 4, 5, 6, 7, 8] print(tuple[3:8]) #Output: ('d', 'e', 'f', 'g', 'h') print(string[-14:6]) #Output: Python #slicing with only step parameter print(list[::-1]) #Output: [9, 8, 7, 6, 5, 4, 3, 2, 1] print(tuple[::2]) #Output: ('a', 'c', 'e', 'g', 'i') print(string[::6]) #Output: P o #slicing with only step parameter print(list[::-1]) #Output: [9, 8, 7, 6, 5, 4, 3, 2, 1] print(tuple[::4]) #Output: ('a', 'e', 'i') print(string[::6]) #Output: P o #the resulting sequences started from the beginning of the original sequence stepped respective values and stoped at the end of the original sequences #slicing with step parameter in combination with start, step parameters print(list[2::3]) #Output: [3, 6, 9] print(tuple[:6:-1]) #Output: ('i', 'h') print(string[5:-3:2]) #Output: nFr
Special cases-:
list = [0,1,2,3,4,5,6,7,8,9,10] #len(list)=11 tuple = ('a','b','c','d','e','f','g','h','i','j','k','l','m') #len(tuple)=13 string = "Python for you: Slicing in python" #len(string)=33 #slicing with no parameters print(list[::]) #Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(tuple[::]) #Output: ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm') print(string[::]) #Output: Python for you: Slicing in python #Default start position is 0 and stop position is len(sequence) #slicing with parameters positions in different direction #start and stop positions in same direction with respect to step print(tuple[3:8]) #Output: ('d', 'e', 'f', 'g', 'h') print(string[-4:-1]) (By default the value of step is 1 and 3+1=4) #Output: tho #-4+1=-3 print(list[5:-3]) #Output: [5, 6, 7] (list[-3-1]=list[8-1]=list[-4]=list[7]=[7]) print(tuple[-8:8]) #Output: ('f', 'g', 'h') (tuple[-8]=tuple[5]=f) print(list[-3:-10:-1]) #Output: [8, 7, 6, 5, 4, 3, 2] (-3-1=-4 belongs to position range -3 to -10) print(tuple[5:1:-1]) #Output: ('f', 'e', 'd', 'c') (5-1=4 belongs to position range 5 to 1) print(string[-11:11:-1]) #Output: gnicilS :uo (string[-11]=string[22]=g) print(tuple[7:-8:-2]) #Output:('h',) (tuple[-8+1]=tuple[-7]=tuple[6]=g (as it is in reverse direction slicing will stop one position after stop position) #start and stop positions in different direction with respect to step print(string[7:0]) #Output: '' (7+1=8 does not belongs to position range 7 to 0) print(tuple[-3:-9:2]) #Output: () (-3+2=-1 does not belongs to range -3 to -9) print(list[-6:2]) #Output: [] (list[-6+1]=list[-5]=list[6], position range direction 6 to 2 does not match with step direction) print(string[25:-31:3]) #Output: '' (string[-31]=string[2], position range direction 25 to 2 does not match with step direction) print(list[4:8:-1]) #Output: [] (4-1=3 does not belongs to position range 4 to 8) print(string[-20:-6:-1]) #Output: '' (-20-1=-21 does not belongs to position range -20 to -6) print(list[5:-3:-1]) #Output: [] (list[-3+1]=list[-2]=list[9] not less than 5 (reverse direction) print(tuple[-7:7:-1]) #Output: () (list[-7]=list[4]=4 ,4-1=3 as step is -1, does not belongs to position range 4 to 7) #slicing parameters with out of bound indexes print(list[14::]) #Output: [] print(list[::14]) #Output: [0] print(tuple[-17::]) #Output: ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm') print(string[-35::40]) #Output: P (string[-33]=P) print(string[-35::40]) #Output: Ph (string[-33]=P & string[-3]=h & -33+30=-3) print(list[:15:]) #Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(tuple[:-18:]) #Output: () print(tuple[:-18:10]) #Output: () print(tuple[:-18:21]) #Output: () print(tuple[:-18:-21]) #Output: ('m',) print(tuple[-13:-18:-21]) #Output: ('a',) print(tuple[-15:-18:-1]) #Output: () (-15-1 = -16 which is out of bound position) print(tuple[15:-18:-21]) #Output: ('m',) #slicing with 0 as step value print(string[::0]) #Output: ValueError: slice step cannot be zero #slicing with 1 position difference between start and stop print(list[0,1]) #Output: [0] print(list[-5:-4]) #Output: [6] print(list[-5:-4:2]) #Output: [6] print(tuple[-9:5]) #Output: e (tuple[-9]=tuple[4]=e) print(string[16:-16]) #Output: i (string[-16]=string[17]) print(tuple[-4:10]) #Output: ('j',) (tuple[-4]=tuple[9]) print(list[10:9:-1]) #Output: [10] print(tuple[-9:-10:-1]) #Output: ('e',) print(string[-12:20:-1]) #Output: i (string[-12]=string[21]) print(list[6:-6:-4]) #Output: [6] (list[-6]=list[5]) #start and stop refering to same position print(tuple[4:4]) #Output: () print(list[-2:-2]) #Output: [] print(string[-20:20:-1]) #Output: ''
Slice()
Slice() is a constructor that creates a slice object to represent the set of indexes(start, stop, step). These slice objects are passed to sequences as indexes for slicing. The syntax of slice() is:
slice(start(optional), stop, step(optional))
When only one parameter value is provided, it takes it to be the stop value since the stop parameter is mandatory to provide in slice function. The default value of start and stop parameters is None. Slice function allows easy creation of a stepwise sub-sequence without doing a complete iteration on the existing sequence.
Creating a slice object in python
a = slice(3) print(a.start) print(a.stop) print(a.step) #Output: None 3 None b = slice(8,9) print(b.start) print(b.stop) print(b.step) #Output: 8 9 None c = slice(1,8,2) print(c.start) print(c.stop) print(c.step) #Output: 1 8 2 d = slice() #Output: TypeError: slice expected at least 1 argument, got 0 e = slice(4,None,1) print(e.start) print(e.stop) print(e.step) #Output: 4 None 1 f = slice(None,7,3) print(f.start) print(f.stop) print(f.step) #Output: None 7 3
Slicing using slice object
Python slice object has no use on its own, it’s useful when used in conjunction with sequences to create a sub-sequence.
s = slice(2,8) print("Betapython"[s]) #Output: tapyth print([1,2,3,4,5,6,7,8,9][s]) #Output: [3, 4, 5, 6, 7, 8] print(('a','b','c','d','e','f','g','h','i','j','k','l','m')[s]) #Output: ('c', 'd', 'e', 'f', 'g', 'h') print("Betapython"[slice(-8,-10)]) #Output: te print([1,2,3,4,5,6,7,8,9][slice(5)]) #Output: [1,2,3,4,5] print(('a','b','c','d','e','f','g','h','i','j','k','l','m')[slice(2,0,-1)]) #Output: ('c', 'b') list = [0,1,2,3,4,5,6,7,8,9,10] tuple = ('a','b','c','d','e','f','g','h','i','j','k','l','m') string = "Betapython" print(list[s]) #Output: [2, 3, 4, 5, 6, 7] print(tuple[slice(5,None,2)]) #Output: ('f', 'h', 'j', 'l') print(string[slice(-5,-8,-2)]) #Output: ya