Python – K elements Reversed Slice
Given List of elements, perform K elements reverse slice.
Input : test_list = [2, 4, 6, 8, 3, 9, 12, 15, 16, 18], K = 3
Output : [18, 16, 15]
Explanation : 3 elements sliced from rear end.
Input : test_list = [2, 4, 6, 8], K = 3
Output : [8, 6, 4]
Explanation : 3 elements sliced from rear end, 8, 6 and 4.
Method #1 : Using list slicing
This is one of the ways in which this task can be performed. In this, we perform reverse slice using reversing and negative slicing capabilities of list slicing.
Python3
test_list = [ 2 , 4 , 6 , 8 , 3 , 9 , 12 , 15 , 16 , 18 ]
print ( "The original list : " + str (test_list))
K = 6
res = test_list[ - K:][:: - 1 ]
print ( "The sliced list : " + str (res))
|
Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The sliced list : [18, 16, 15, 12, 9, 3]
Time Complexity: O(n), where n is the elements of list
Auxiliary Space: O(n), where n is the size of list
Method #2 : Using islice() + reversed()
This is functional approach to solve this problem. In this, we perform slicing using islice(), and then list is reversed using reversed().
Python3
from itertools import islice
test_list = [ 2 , 4 , 6 , 8 , 3 , 9 , 12 , 15 , 16 , 18 ]
print ( "The original list : " + str (test_list))
K = 6
res = list (islice( reversed (test_list), K))
print ( "The sliced list : " + str (res))
|
Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The sliced list : [18, 16, 15, 12, 9, 3]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method 3 : using the deque data structure from the collections module
Explanation:
- We import the deque data structure from the collections module in Python.
- We create a deque object containing the last K elements of the original list.
- We reverse the deque object using the reverse() method.
- Finally, we convert the deque object to a list using the list() method.
Python3
from collections import deque
test_list = [ 2 , 4 , 6 , 8 , 3 , 9 , 12 , 15 , 16 , 18 ]
print ( "The original list : " + str (test_list))
K = 6
res = deque(test_list[ - K:])
res.reverse()
res = list (res)
print ( "The sliced list : " + str (res))
|
Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The sliced list : [18, 16, 15, 12, 9, 3]
Time complexity: O(K)
Auxiliary space: O(K)
METHOD 4:Using loop and reverse.
APPROACH:
This program takes a list of integers as input and a number ‘k’ indicating how many elements to slice from the end of the list, then creates a new list containing the sliced elements in reverse order.
ALGORITHM:
1. Initialize an empty list to hold the sliced elements.
2. Loop through the original list in reverse order using range function with step -1.
3. Append the current element to the sliced list and decrement k by 1.
4. If k is equal to 0, break out of the loop.
5. Print the sliced list.
Python3
original_list = [ 2 , 4 , 6 , 8 , 3 , 9 , 12 , 15 , 16 , 18 ]
k = 6
sliced_list = []
for i in range ( len (original_list) - 1 , - 1 , - 1 ):
if k = = 0 :
break
sliced_list.append(original_list[i])
k - = 1
print ( "The sliced list:" , sliced_list)
|
Output
The sliced list: [18, 16, 15, 12, 9, 3]
Time Complexity:
The time complexity of this program is O(k), where k is the number of elements to slice.
Space Complexity:
The space complexity of this program is O(k), since we are creating a new list to hold the sliced elements.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...