# Slicing Tricks — lists, str, bytes

is preferred over a[5:len(a)] ????Using negative numbers for slicing is helpful for doing offsets relative to the end of the list.

# a = [1, 2, 3, 4, 5, 6, 7, 8]a[:] # [1, 2, 3, 4, 5, 6, 7, 8]a[:5] # [1, 2, 3, 4, 5]a[:-1] # [1, 2, 3, 4, 5, 6, 7]a[4:] # [5, 6, 7, 8]a[-3:] # [6, 7, 8]a[2:5] # [3, 4, 5]a[2:-1] # [3, 4, 5, 6, 7]a[-3:-1] # [6, 7]Slicing deals properly with start and end indexes that are beyond the boundaries of the list.

first_twenty_items = a[:20]last_twenty_items = a[-20:]In contrast, accessing the same index directly will cause an exception.

# a = [1, 2, 3, 4, 5, 6, 7, 8]a[20]>>>IndexError: list index out of rangeNOTE: we can get surprising results from slicing in some scenarios.

Try a[-0:] ????The result of slicing a list is a whole new list.

References to the objects from the original list are maintained.

Modifying the result of slicing won’t affect the original list.

# a = [1, 2, 3, 4, 5, 6, 7, 8]b = a[4:]print ('before: ', b)b[1] = 99print ('after: ', b)print ('no change: ', a)>>>before: [5, 6, 7, 8]after: [5, 99, 7, 8]no change: [1, 2, 3, 4, 5, 6, 7, 8]When used in assignments, slices will replace the specified range in the original list.

The length of slice assignments don’t need to be the same.

The list will grow or shrink to accommodate the new values.

# a = [1, 2, 3, 4, 5, 6, 7, 8]print ('before: ', a)a[2:7] = [99, 22, 14]print ('after: ', a)>>>before: [1, 2, 3, 4, 5, 6, 7, 8]after: [1, 2, 99, 22, 14, 8]Using slicing, we can also create copies of list>>> a[1, 2, 3, 4, 5, 6, 7, 8]>>> b = a[:]>>> b is aFalse>>> b[1, 2, 3, 4, 5, 6, 7, 8]>>> c = a>>> c is aTrue>>> c[1, 2, 3, 4, 5, 6, 7, 8]>>> c = [10, 20, 30]>>> a[10, 20, 30]Specifying start, end, and stride in a slice can be used to reverse a list.

# a = [1, 2, 3, 4, 5, 6, 7, 8]>>> b = a[::-1]>>> b[8, 7, 6, 5, 4, 3, 2, 1]But, how much useful are negative strides besides -1.

Consider the following example.

a # [1, 2, 3, 4, 5, 6, 7, 8]a[::2] # [1, 3, 5, 7]a[::-2] # [8, 6, 4, 2]Here, ::2 means select every second item starting at the beginning whereas ::-2 means select every second item starting at the end and moving backwards.

It will only complicate things if we have slicing like 2::2, -2::-2, -2:2:-2, 2:2:-2 ????a[2::2] # [3, 5, 7]a[-2::-2] # [7, 5, 3, 1]a[-2:2:-2] # [7, 5]a[2:2:-2] # []stride part of slicing syntax can be confusing, especially if the stride is negative.

Using it can make our code less maintainable.

Key HighlightsSlicing can not only be used for accessing range of elements but also for clearing, reversing and copying lists.

Avoid using negative stride along with start and end indexes, if possible.