Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – K elements Reversed Slice

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




# 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))


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




# 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))


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




# 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))


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  # 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)


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments