How to Get Last 2, 3, 4, 5, 10, etc Elements from List in Python?
Hi Friends,
In this tutorial, you will discover python get last 10 elements from list. Iรขโฌโขm going to show you about python get last 4 elements from list. This example will help you how to get last 2 elements from list in python. you can understand a concept of how to get last 3 element from list in python. follow the below example for python list get last 5 elements.
I will give you 5 examples of how to get last two, three, four, five, ten elements from list. you can view one by one example that way you can use it what you need. we will use [-N:] to get last elements. let's see the examples:
You can use these examples with python3 (Python 3) version.
Example: Python Get Last 2 Elements from List
main.py
# Create New List with Item myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Python Get Last 2 Elements from List newList = myList[-2:] print(newList)
Output:
[11, 12]
Example: Python Get Last 3 Elements from List
main.py
# Create New List with Item myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Python Get Last 3 Elements from List newList = myList[-3:] print(newList)
Output:
[10, 11, 12]
Example: Python Get Last 4 Elements from List
main.py
# Create New List with Item myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Python Get Last 4 Elements from List newList = myList[-4:] print(newList)
Output:
[9, 10, 11, 12]
Example: Python Get Last 5 Elements from List
main.py
# Create New List with Item myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Python Get Last 5 Elements from List newList = myList[-5:] print(newList)
Output:
[8, 9, 10, 11, 12]
Example: Python Get Last 10 Elements from List
main.py
# Create New List with Item myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Python Get Last 10 Elements from List newList = myList[-10:] print(newList)
Output:
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
I hope it can help you...