[Start:Stop:Step]
>>> my_list = list(range(20)) >>> my_list [0,1,2,3, ... , 19] >>> my_list[::2] [0,2,4,6,8, ... ,18] >>> my_list[2::2] [2,4,6,8, ... ,18]
with Strings too
>>> "Oklahoma"[::2] 'Olhm' >>> "Oklahoma"[::-1] 'amohalkO'
Backwards
>>> my_list[::-2] [19,17,15,13, ... , 3, 1] >>> my_list[2:8:-1] [] # Logically, you cannot get to 8 counting from 2 backwards! >>> my_list[8:2:-1] # This is the right way to do it. [8,7,6,5,4,3] >>> my_list[7-1] [7,6,5,4,3,2] >>> my_list[-2] 18
Delete & Replace Slices
>>> my_list = [1,2,'a','b','c','d',5,6,7,'f','g','h',8,9,'j']
Using del
>>> del my_list[:2] >>> my_list ['a','b','c','d',5,6,7,'f','g','h',8,9,'j']
Replace Slice with a Slice
>>> my_list[4:7] = ['e','f'] >>> my_list ['a','b','c','d','e','f','f','g','h',8,9,'j'] >>> my_list.remove('f') ['a','b','c','d','e','f','g','h',8,9,'j']
Replace Slice with an item
>>> my_list[8:10] = ['i'] >>> my_list ['a','b','c','d','e','f','g','h','i','j']