Question #794   Submitted by Answiki on 11/02/2021 at 06:25:02 PM UTC

In Python, how to slice a list from a given index to the end?

Answer   Submitted by Answiki on 11/02/2021 at 06:32:36 PM UTC

In Python, the simplest way to slice a list from a given index to the end is to slice with indexes list[n:]:

>>> myList=[10,20,30,40,50]
>>> myList[2:]
[30, 40, 50]

Note that the index can be referenced from the end of the list list[-n:]:

>>> myList=[10,20,30,40,50]
>>> myList[-3:]
[30, 40, 50]

3 events in history
Answer by Answiki on 11/02/2021 at 06:32:36 PM

In Python, the simplest way to slice a list from a given index to the end is to slice with indexes list[n:]:

>>> myList=[10,20,30,40,50]
>>> myList[2:]
[30, 40, 50]

Note that the index can be referenced from the end of the list list[-n:]:

>>> myList=[10,20,30,40,50]
>>> myList[-3:]
[30, 40, 50]

Answer by Answiki on 11/02/2021 at 06:29:11 PM

In Python, the simplest way to slice a list from a given index to the end is to slice with indexes list[-n:] :

>>> myList=[10,20,30,40,50]
>>> myList[-3:]
[30, 40, 50]

Question by Answiki 11/02/2021 at 06:25:02 PM
In Python, how to slice a list from a given index to the end?
# ID Query URL Count
0 2838 -1: index slicing in python https://en.ans.wiki/794/in-python-how-to-slice-a-list-from-a-given-index-to-the-end 2

Icons proudly provided by Friconix.