Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must have the knowledge to perform the same. This article discusses ways to fetch the last N elements of list. Let’s discuss certain solution to perform this task.
Method #1 : Using list slicing
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 rear end.
Python3
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
N = 5
res = test_list[ - N:]
print ( "The last N elements of list are : " + str (res))
|
Output :
The original list : [4, 5, 2, 6, 7, 8, 10]
The last N elements of list are : [2, 6, 7, 8, 10]
Time Complexity: O(n) where m and n is the number of elements in the list. list slicing performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using islice() + reversed()
The inbuilt 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.
Python3
from itertools import islice
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
N = 5
res = list (islice( reversed (test_list), 0 , N))
res.reverse()
print ( "The last N elements of list are : " + str (res))
|
Output :
The original list : [4, 5, 2, 6, 7, 8, 10]
The 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
Method #3 : Using loop
Python3
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
N = 5
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))
|
Output
The original list : [4, 5, 2, 6, 7, 8, 10]
The last N elements of list are : [2, 6, 7, 8, 10]
Time Complexity : O(N)
Auxiliary Space : O(1)
Method #4: Using a generator function
You can define a generator function that yields the last N elements of a list.
Python3
def get_last_n(lst, n):
yield lst[ len (lst) - n: len (lst)]
test_list = [ 4 , 5 , 2 , 6 , 7 , 8 , 10 ]
print ( "The original list : " + str (test_list))
print ( "The last N elements of list are : " + str ( list (get_last_n(test_list, 5 ))))
|
Output
The original list : [4, 5, 2, 6, 7, 8, 10]
The last N elements of list are : [[2, 6, 7, 8, 10]]
Time Complexity : O(N)
Auxiliary Space : O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Apr, 2023
Like Article
Save Article