Range of a List in Python

The slice notation in Python is easy to understand. If you want to get a range of a list, you have to give the start and end values.

The result is the list of elements between these two values.

['This', 'is', 'just', 'a', 'string']

The indexing starts from 0, therefore the word ‘This’ is the second element (1 is the second after 0). the number 6 represents the sixth element from the beginning of the list: ‘string’.

Other slice notation examples

The colon separates the beginning and the end of the list.

There are three additional examples.

The first one prints a range from the third element to the end of the string

The second example starts from the beginning to the end of the string.

The third one prints the whole list.

['is', 'just', 'a', 'string', '!']
['Hey,', 'This', 'is', 'just', 'a']
['Hey,', 'This', 'is', 'just', 'a', 'string', '!']

Get a range to the nth element of the list

So far, we have been using positive numbers for list indexes, but negative ranges are also possible.

To get a range from the second element to the third from the end, you can use this code.

This is the result you will get.

['This', 'is', 'just', 'a']

Get every nth element from the list

So far so good, but there is much more you can do with slice notation in Python.

There is also a way to get every nth element from the list. You can supply also the step value as the third number separated by a colon.  By default, this number is 1.

Both lines return the same result.

['Hey,', 'This', 'is', 'just', 'a']
['Hey,', 'This', 'is', 'just', 'a']

If you want to get every other item, you should use 2 as the third number.

This code returns every other element from the beginning to the fifth element of the list.

['Hey,', 'is', 'a']

Getting reversed range from a list

The third part can also be represented by a negative number. When you use the negative number, Python is going to return the string in the reversed order.

If you are using negative numbers as the “step” value, you have to switch the start with the stop number.

This is the result.

['string', 'a', 'just', 'is', 'This', 'Hey,']

Of course, you can also return every nth element as reversed.

This is the result.

['string', 'just', 'This']

Index out of range

You probably know what happens when you try to access a list value that doesn’t exist.

That’s right, you will get an error informing you that there is no such index inside the list.

IndexError: list index out of range

But look what happens when you try to display an index that is outside the list.

If you put the end number that is outside the scope, Python will ignore additional indexes, as if nothing happened.

['just', 'a', 'string', '!']

It’s even weirder when the starting position is also outside the range.

The result is just an empty string.

[]

Handle index out of range error

Let’s create a simple function that is going to handle this exception, and return an error.

The function strict_range is going to return range only if it’s inside a list. If it doesn’t meet this requirement it’s going to raise the index error.