Python - K Elements Reversed Slice


The reverse slice is defined by creating a slice that begins with the length of the string and ends with the 0th index. For reversing the list element it will use the negative value notation, where we can get a reversed order of the original list element. In Python, we have some built-in functions such as append(), len(), and, range() will be used to solve the K elements Reversed Slice.

Syntax

The following syntax is used in the examples-

append()

This built-in method in Python can be used to add the element at the end of the list.

len()

The built-in function len() is used to return the length of the object.

range()

The range() is an in-built function in Python that returns the sequence of numbers according to a given range.

reversed()

The reversed() is an in-built function in Python that returns the given list element in the form of reverse order.

Using Slicing

In the following example, we will start the program with a function named rev_slice that accepts the parameters- r_list and k to receive the input values. Next, use the slicing notation that will work on reversing the element according to K value and get the desired result.

Example

def rev_slice(r_list, k):
    return r_list[-k:][::-1]
test_list = [2, 4, 20, 40, 60, 80]
k = 2
output = rev_slice(test_list, k)
print("The reverse slice based on the K element:\n", output)

Output

 The reverse slice based on the K element: [80, 60]

Using reversed()

In the following example, we will use the recursive function that calls itself whenever it is required. Next, use the built-in function reversed() that accepts the parameter- t_list[-k:] to reverse all the elements in the form of a list.

Example

def rev_slice(t_list, k):
    return list(reversed(t_list[-k:]))
test_list = [1, 2, 3, 4, 5]
# Initialize the K value
k = 2
# Calling function
output = rev_slice(test_list, k)
print("The following K element reverse:", output)

Output

 The following K element reverse: [5, 4]

Using list Comprehension

In the following example, the program uses list comprehension that uses for loop where variable i iterate through the input list, and using built-in function range() it simplifies the sequence of indexes from start to end. To reverse the K element it will use the slice notation[::-1].

Example

def rev_slice(t_list, k):
    return [t_list[i] for i in range(len(t_list) - k, len(t_list))][::-1]
test_list = [2, 4, 20, 40, 11, 12]
k = 4
res = rev_slice(test_list, k)
print("The K reverse element are-",res)

Output

 The K reverse element are- [12, 11, 40, 20]

Using range() and append()

In the following example, we will start the program by using the recursive function rev_slice() that accepts two parameters- t_list and k that will receive the input list element and k value to work on the reverse slice. Next, use the empty list in the variable reversed_slice that will be used to store the final result. Then using the for loop where variable i iterate through the input list with the help of some built-in function such as range() and len(). Moving ahead to return the reversed_slice to get the specific list element.

Example

def rev_slice(t_list, k):
    reversed_slice = []
    for i in range(len(t_list) - 1, len(t_list) - k - 1, -1):
        reversed_slice.append(t_list[i])
    return reversed_slice
test_list = [10, 20, 30, 40, 50, 60, 70, 80]
k = 3
res = rev_slice(test_list, k)
print("Following K reverse element:", res)

Output

 Following K reverse element: [80, 70, 60]

Conclusion

We explored the K elements reversed slice in Python that provides a simple way to get the subset of the list. It is used to deal with a large dataset to extract the exact portion of list in the reverse order. This type of code solves the specific tasks that require to analyze the data in a reverse manner.

Updated on: 16-Aug-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements