# Range of a List in Python

Contents:

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 value.

``````lst = ['Hey,', 'This', 'is', 'just', 'a', 'string', '!']
print(lst[1:6])
``````

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.

``````print(lst[2:])
print(lst[:5])
print(lst[:])
``````

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.

``print(lst[1:-2])``

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.

``````print(lst[0:5])
print(lst[0:5: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.

``print(lst[:5:2])``

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.

``print(lst[5::-1])``

This is the result.

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

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

``print(lst[5::-2])``

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.

``print(lst)``

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.

``print(lst[3:10])``

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

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

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

``print(lst[10:15])``

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.

``````def strict_range(lst, start=None, end=None):
length = lst.__len__()
if start is None:
start = 0
if end is None:
end = length
if end < 0:
end = length - end
if not (0 <= start < length and 0 <= end <= length):
raise IndexError()
else:
return lst[start:end]

lst = ['Hey,', 'This', 'is', 'just', 'a', 'string', '!']
lst = strict_range(lst, 1, 5)
print(lst)
``````

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.