Python - K elements Reversed Slice
Last Updated :
08 May, 2023
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
# Python3 code to demonstrate working of
# K elements Reversed Slice
# Using list slicing
# initializing list
test_list = [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 6
# using double slice to solve problem.
# "-" sign for slicing from rear
res = test_list[-K:][::-1]
# printing result
print("The sliced list : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# K elements Reversed Slice
# Using K elements Reversed Slice
from itertools import islice
# initializing list
test_list = [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 6
# using reversed and islice to slice
# and then perform reverse
res = list(islice(reversed(test_list), K))
# printing result
print("The sliced list : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# K elements Reversed Slice
# Using deque
from collections import deque
# initializing list
test_list = [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 6
# using deque to slice and reverse
res = deque(test_list[-K:])
res.reverse()
# converting deque to list
res = list(res)
# printing result
print("The sliced list : " + str(res))
OutputThe 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 # number of elements to slice
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)
OutputThe 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.
Similar Reads
Python | K elements Slicing
We often come to the situations in which we need to extract initial K elements of list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar size. Letâs discuss certain ways in wh
4 min read
Python - Rear Kth elements
Given a list, the task is to extract all Kth elements from rear end. Input : test_list = [3, 4, 5, 2, 1], K = 2 Output : [1, 5, 3] Explanation : Every 2nd elements are extracted from rear starting from 1. Input : test_list = [3, 4, 5], K = 1 Output : [5, 4, 3] Explanation : Every elements are extrac
3 min read
Python | Remove Front K elements
We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 min read
Python - Surrounding elements to K
Given Matrix, from each row, get surrounding elements of K, if present. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1], [6, 1, 2]], K = 6Â Output : [[7, 3], [5], [], [1]] Explanation : All elements surrounded by 6 are extracted.Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1], [6, 1, 2]], K = 1
5 min read
Python | Reverse Incremental String Slicing
Sometimes, while working with Python strings, we can have a problem in which we need to perform the slice and print of strings in reverse order. This can have applications in day-day programming. Let us discuss certain ways in which this task can be performed. Method #1: Using loops This is the brut
4 min read
Python | Rear elements from Tuple Strings
Yet another peculiar problem that might not be common, but can occur in python programming while playing with tuples. Since tuples are immutable, they are difficult to manipulate and hence knowledge of possible variation solutions always helps. This article solves the problem of extracting only the
5 min read
Python - Reform K digit elements
Given the Python list, reform the element to have K digits in a single element. Input : test_list = [223, 67, 332, 1, 239, 2, 931], K = 2 Output : [22, 36, 73, 32, 12, 39, 29, 31] Explanation : Elements reformed to assign 2 digits to each element. Input : test_list = [223, 67, 3327], K = 3 Output :
3 min read
Python | Reverse sign of each element in given list
Given a list of integers, write a Python program to reverse the sign of each element in given list. Examples: Input : [-1, 2, 3, -4, 5, -6, -7] Output : [1, -2, -3, 4, -5, 6, 7] Input : [-5, 9, -23, -2, 7] Output : [5, -9, 23, 2, -7] Methods #1: List comprehension Python3 # Python3 program to Conver
3 min read
Python - Reverse Slicing of given string
Reverse slicing in Python is a way to access string elements in reverse order using negative steps.Using Slicing ([::-1])Using slicing with [::-1] in Python, we can reverse a string or list. This technique works by specifying a step of -1, which starts at the end of the sequence and moves backward,
1 min read
Python | Retain K Front and Rear elements
Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element retention and deletion of rest elements. It is a good utility whose solution can be useful to have. Letâs discuss certain ways in
8 min read