Slice lists in Python

Get sublist

>>> myList=[10,20,30,40,50]
>>> myList[1:4]
[20, 30, 40]

Get sublist from the beginning

>>> myList=[10,20,30,40,50]
>>> myList[:2]
[10, 20]

Get sublist from the end

>>> myList=[10,20,30,40,50]
>>> myList[-3:]
[30, 40, 50]

See also


Last update : 06/11/2018