# Droping data with range of negitive indexes

I’m working with list and list of list. I need to be able to address the data from both ways (right to left and left to right). This is needed when you have a list of lists AND need to address the last ‘sub’ list of the list of lists. Just groups of data. But we need to address the complete last dataset (sub list). Even if it is the first data sub list or the 50th. As it is now we lose data.

``````data = [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 'test 3b',
'Lumpy', 0, 0, 45, 18, 24, 19, 9]
a = -1
b = -21
# Example:  data[b:a]

print('this should be 0 ', data)
print('this should be 9 ', data)

print('this should be 9 ', data[-1])
print('this should be 0 ', data[-21])
print('this should be 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "test 3b", '
'"Lumpy", 0, 0, 45, 18, 24, 19, 9 ', data[-21:-1])  # missing 9 at end

OUTPUT:
this should be 0  0
this should be 9  9
this should be 9  9
this should be 0  0
this should be 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "test 3b", "Lumpy", 0, 0, 45, 18, 24, 19, 9 output  [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 'test 3b', 'Lumpy', 0, 0, 45, 18, 24, 19]
``````

It drops data. I need help understanding this.
Thanks

If you do slice then start is always included, and the end always excluded. Also, you don’t need slice if you want whole list:

``````>>> data = ['spam', 'ham', 'eggs']
>>> data[-3:-1]        # item at index -1 excluded
['spam', 'ham']
>>> data[-3:]          # all items starting from index -3
['spam', 'ham', 'eggs']
>>> data               # whole list
['spam', 'ham', 'eggs']
``````

I need the whole group between -21 and -1. -1 is to be the last data point of the list.
example: # [1, 5, ‘list’, j, 16, 23]
get data = [-4:-1] # result j, 16, 23

NOT get data = [-4:-1] # result j, 16 wrong. -1 should be the last data point 23

Thanks
Does this mean we can’t address the last data point of the data? ?How is that? Do we have to do this in two steps, first to get all the -21:-1 and then len(data) append?

An end of -1 does not include the last element.
To include the last element in a slice you can omit the end value or use None (sadly 0 does not work).

Consider slicing from the front of a list:

``````:>>> x = list(range(10))
:>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
:>>> x[0:2]
[0, 1]
``````

x is not included.

Now for the -ive case you use to include the last element in the list you cannot use -1 that is the last but 1 element. You can omit the end-index or use None.

If you are calculating the indices then you need to know to replace a calculated end of 0 with None.

``````:>>> x[-3:0]
[]
:>>> x[-3:None]
[7, 8, 9]
:>>> x[-3:]
[7, 8, 9]
:>>> x[-1:None]

:>>>
``````

In mathematics they talk about open and closed intervals - see Interval (mathematics) - Wikipedia.

The slice is [start-index, end-index) it include the start-index but not the end-index.

Thanks! Got it. They need to put this in the tutorials where this is explained about using - reference… I knew that there had to be a way.