What is the syntax to create a sublist of a list?

<list name>[<slice start index>:<slice end index>]
#start index is inclusive
#end index is exclusive
#examples
>>> flowers=['tulips','roses','desk','chair','orchids']

>>> favFlowers=flowers[0:2]
>>> print(favFlowers)
#output
['tulips', 'roses']

>>> notFlowers=flowers[3:4]
>>> print(notFlowers)
#output
['chair']

>>> allExceptLast=flowers[:4]
>>> print(allExceptLast)
#output
['tulips', 'roses', 'desk', 'chair']

>>> allFlowers=flowers[:]
>>> print(allFlowers)
#output
['tulips', 'roses', 'desk', 'chair', 'orchids']

 

Advertisements