Python | Get Last N Elements from Given List
Last Updated :
01 Oct, 2024
This article discusses ways to fetch the last N elements of a list in Python.
Example: Using list Slicing
Python
test_list = [4, 5, 2, 6, 7, 8, 10]
# initializing N
N = 5
# using list slicing
res = test_list[-N:]
print(str(res))
Explaination: This problem can be performed in 1 line rather than using a loop using the list-slicing functionality provided by Python. Minus operator specifies slicing to be done from the rear end.
Let's discuss certain solutions to perform this task.
Get the Last N Elements of a List using islice()
The inbuilt islice() functions can also be used to perform this particular task. The islice function can be used to get the sliced list and reversed function is used to get the elements from rear end.
Python
from itertools import islice
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
# initializing N
N = 5
# using islice() + reversed()
# Get last N elements from list
res = list(islice(reversed(test_list), 0, N))
res.reverse()
# print result
print("The last N elements of list are : " + str(res))
OutputThe last N elements of list are : [2, 6, 7, 8, 10]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Take Last N Elements using Loop
This code extracts the last N elements from a list. It first reverses the list using slicing ([::-1]), then iterates over the first N elements, appending them to a new list res. Finally, it reverses res to restore the original order and prints the last N elements.
Python
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
# initializing N
N = 5
# Get last N elements from list
x=test_list[::-1]
res=[]
i=0
while(i<N):
res.append(x[i])
i+=1
res.reverse()
print("The last N elements of list are : " + str(res))
OutputThe last N elements of list are : [2, 6, 7, 8, 10]
Python Last N Elements of List using Generator
You can define a generator function that yields the last N elements of a list.
Python
def get_last_n(lst, n):
yield lst[len(lst)-n:len(lst)]
# initializing list
test_list = [4, 5, 2, 6, 7, 8, 10]
print("The last N elements of list are : " + str(list(get_last_n(test_list, 5))))
OutputThe last N elements of list are : [[2, 6, 7, 8, 10]]
Time Complexity : O(N)
Auxiliary Space : O(1)
Similar Reads
Get first and last elements of a list in Python The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way
3 min read
Get the Last Element of List in Python In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list:Input: list = [1, 3, 34, 12, 6]Output: 6Explanation: Last element of the list l in the above example is 6.Let's explore various methods of doing it in Python:1. Using Neg
2 min read
Python | Remove given element from list of lists The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read
Python | Indices of N largest elements in list Sometimes, while working with Python lists, we can have a problem in which we wish to find N largest elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let's discuss a certain way to fi
5 min read
Shift Last Element to First Position in list - Python The task of shifting the last element to the first position in a list in Python involves modifying the order of elements such that the last item of the list is moved to the beginning while the rest of the list remains intact. For example, given a list a = [1, 4, 5, 6, 7, 8, 9, 12], the goal is to sh
3 min read