# Indexing and Slicing

## Syntax

• obj[start:stop:step]
• slice(stop)
• slice(start, stop[, step])

## Parameters

ParamerDescription
`obj`The object that you want to extract a "sub-object" from
`start`The index of `obj` that you want the sub-object to start from (keep in mind that Python is zero-indexed, meaning that the first item of `obj` has an index of `0`). If omitted, defaults to `0`.
`stop`The (non-inclusive) index of `obj` that you want the sub-object to end at. If omitted, defaults to `len(obj)`.
`step`Allows you to select only every `step` item. If omitted, defaults to `1`.

## Remarks

You can unify the concept of slicing strings with that of slicing other sequences by viewing strings as an immutable collection of characters, with the caveat that a unicode character is represented by a string of length 1.

In mathematical notation you can consider slicing to use a half-open interval of `[start, end)`, that is to say that the start is included but the end is not. The half-open nature of the interval has the advantage that `len(x[:n])` = `n` where `len(x)` > =`n`, while the interval being closed at the start has the advantage that `x[n:n+1]` = `[x[n]]` where `x` is a list with `len(x) >= n`, thus keeping consistency between indexing and slicing notation.

## Basic Slicing

For any iterable (for eg. a string, list, etc), Python allows you to slice and return a substring or sublist of its data.

Format for slicing:

where,

• `start` is the first index of the slice. Defaults to 0 (the index of the first element)
• `stop` one past the last index of the slice. Defaults to len(iterable)
• `step` is the step size (better explained by the examples below)

Examples:

In addition, any of the above can be used with the step size defined:

Indices can be negative, in which case they're computed from the end of the sequence

Step sizes can also be negative, in which case slice will iterate through the list in reverse order:

This construct is useful for reversing an iterable

Notice that for negative steps the default `end_index` is `None` (see http://stackoverflow.com/a/12521981 )

## Basic Indexing

Python lists are 0-based i.e. the first element in the list can be accessed by the index `0`

You can access the second element in the list by index `1`, third element by index `2` and so on:

You can also use negative indices to access elements from the end of the list. eg. index `-1` will give you the last element of the list and index `-2` will give you the second-to-last element of the list:

If you try to access an index which is not present in the list, an `IndexError` will be raised:

## Indexing custom classes: __getitem__, __setitem__ and __delitem__

This allows slicing and indexing for element access:

While setting and deleting elements only allows for comma seperated integer indexing (no slicing):

## Making a shallow copy of an array

A quick way to make a copy of an array (as opposed to assigning a variable with another reference to the original array) is:

Let's examine the syntax. `[:]` means that `start`, `end`, and `slice` are all omitted. They default to `0`, `len(arr)`, and `1`, respectively, meaning that subarray that we are requesting will have all of the elements of `arr` from the beginning until the very end.

In practice, this looks something like:

As you can see, `arr.append('d')` added `d` to `arr`, but `copy` remained unchanged!

Note that this makes a shallow copy, and is identical to `arr.copy()`.

## Reversing an object

You can use slices to very easily reverse a `str`, `list`, or `tuple` (or basically any collection object that implements slicing with the step parameter). Here is an example of reversing a string, although this applies equally to the other types listed above:

Let's quickly look at the syntax. `[::-1]` means that the slice should be from the beginning until the end of the string (because `start` and `end` are omitted) and a step of `-1` means that it should move through the string in reverse.

## Slice assignment

Another neat feature using slices is slice assignment. Python allows you to assign new slices to replace old slices of a list in a single operation.

This means that if you have a list, you can replace multiple members in a single assignment:

The assignment shouldn't match in size as well, so if you wanted to replace an old slice with a new slice that is different in size, you could:

It's also possible to use the known slicing syntax to do things like replacing the entire list:

Or just the last two members:

## Slice objects

Slices are objects in themselves and can be stored in variables with the built-in `slice()` function. Slice variables can be used to make your code more readable and to promote reuse.