Overview
- Slicing operations on a list using a begin index and end index returns list slices containing shallow copies of list elements.
Example
# A list of things as seen from the window
view = ["sky", "cloud", "rain", "rainbow", "sun", "aircraft", "trees"]
print("List elements:");
print(view);
# Take few elements from the middle
slice = view[2:4]
print(slice)
# Take few elements from the last
slice = view[5:7]
print(slice)
# Take few elements from the beginning
slice = view[0:2]
print(slice)
# Extract few elements from the beginning
slice = view[-6:-3]
print(slice)Output
List elements:
['sky', 'cloud', 'rain', 'rainbow', 'sun', 'aircraft', 'trees']
['rain', 'rainbow']
['aircraft', 'trees']
['sky', 'cloud']
['cloud', 'rain', 'rainbow']