3.1.6.4 Operations on lists | slices

Slices: continued

If you omit the start in your slice, it is assumed that you want to get a slice beginning at the element with index 0.
In other words, the slice of this form:
myList[:end]
is a more compact equivalent of:
myList[0:end]
Look at the snippet below:
myList = [10, 8, 6, 4, 2] newList = myList[:3] print(newList)
This is why its output is: [10, 8, 6].
Similarly, if you omit the end in your slice, it is assumed that you want the slice to end at the element with the index len(myList).
In other words, the slice of this form:
myList[start:]
is a more compact equivalent of:
myList[start:len(myList)]
Look at the following snippet:
myList = [10, 8, 6, 4, 2] newList = myList[3:] print(newList)
Its output is therefore: [4, 2].

  • Console 

Comments

Popular Posts