Being familiar with the concept of queue, which follows the FIFO rule, i.e first in first out, that suggests a front removal and rear insertion. This has been discussed many times. But sometimes we need to perform the exact opposite of this and we need to perform the append at the front and remove the element from the rear. Let’s discuss certain ways in which this can be done.
Method #1: Using + operator and list slicing
These operators can be used to perform this particular task. The append operation can be done with the help of + operator and the removal of rear can be done using the conventional list slicing.
Python3
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print (& quot
The original list : & quot
+ str (test_list))
res = [ 13 ] + test_list[: - 1 ]
print (& quot
The list after append and removal : & quot
+ str (res))
|
Output : The original list : [4, 5, 7, 3, 10]
The list after append and removal : [13, 4, 5, 7, 3]
Time Complexity: O(n) , where n is the number of elements in the list.
Auxiliary Space: O(m), where m is the length of the first non-empty string.
Method #2 : Using collections.deque() The doubly-ended queue can be used to perform this particular task in which is supported by python using the collection library, the appendleft and pop methods of queue function can be used to perform this job.
Python3
from collections import deque
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print (& quot
The original list : & quot
+ str (test_list))
res = deque(test_list)
res.appendleft( 13 )
res.pop()
res = list (res)
print (& quot
The list after append and removal : & quot
+ str (res))
|
Output : The original list : [4, 5, 7, 3, 10]
The list after append and removal : [13, 4, 5, 7, 3]
Time Complexity: O(n) , where n is the number of elements in the list.
Auxiliary Space: O(m), where m is the length of the first non-empty string.
Method #3 : Using pop() and insert() methods
Python3
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print ( "The original list : " + str (test_list))
test_list.pop()
test_list.insert( 0 , 13 )
print ( "The list after append and removal : " + str (test_list))
|
OutputThe original list : [4, 5, 7, 3, 10]
The list after append and removal : [13, 4, 5, 7, 3]
Method #4 : Using islice
You can use the islice function from the itertools module to perform append at the front and remove from the rear in Python. islice allows you to slice an iterable by index and return an iterator over the sliced elements.
Here’s an example of how you can use islice to append an element at the front and remove an element from the rear of a list:
Python3
from itertools import islice
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print ( "The original list:" , test_list)
res = [ 13 ] + list (islice(test_list, 0 , len (test_list) - 1 ))
print ( "The list after append and removal:" , res)
|
OutputThe original list: [4, 5, 7, 3, 10]
The list after append and removal: [13, 4, 5, 7, 3]
The time complexity of the code snippet you provided is O(n), where n is the length of the list. This is because the islice function iterates over the entire list and creates a new list with the sliced elements, which takes O(n) time.
The space complexity of the code snippet is also O(n), since the islice function creates a new list with the sliced elements, which takes up O(n) space.
Method#5: Using List Comprehension.
Python3
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print ( "The original list : " + str (test_list))
res = [ 13 ] + [test_list[i] for i in range ( len (test_list) - 1 )]
print ( "The list after append and removal : " + str (res))
|
OutputThe original list : [4, 5, 7, 3, 10]
The list after append and removal : [13, 4, 5, 7, 3]
Time Complexity: O(n)
Space Complexity: O(n)
Method#6 Using NumPy:
Python3
import numpy as np
test_array = np.array([ 4 , 5 , 7 , 3 , 10 ])
print ( "The original array : " , test_array)
res = np.concatenate(([ 13 ], np.delete(test_array, - 1 )))
print ( "The array after append and removal : " , res)
|
Output:
The original array: [4, 5, 7, 3, 10]
The array after append and removal : [13 4 5 7 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#7 Using list slicing:
Python3
def append_front_remove_rear(lst, front):
return [front] + lst[: - 1 ]
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print ( "The original array : " , test_list)
front = 13
print (append_front_remove_rear(test_list, front))
|
OutputThe original array : [4, 5, 7, 3, 10]
[13, 4, 5, 7, 3]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #8: Use the built-in list.pop() and list.insert()
Step-by-step approach:
- Define a function append_front_remove_rear that takes two arguments: a list lst and a new element front to add to the front of the list.
- Use the pop() method to remove the last element from the input list lst. This method modifies the list in place, so there is no need to create a new list.
- Use the insert() method to insert the new element front at the beginning of the input list lst. This method also modifies the list in place.
- Return the modified list lst as the output of the function.
- Define a test list test_list with some initial elements.
- Define a new element front to add to the front of the test list.
- Call the append_front_remove_rear function with test_list and front as arguments, and print the result.
Python3
def append_front_remove_rear(lst, front):
lst.pop()
lst.insert( 0 , front)
return lst
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print ( "The original array : " , test_list)
front = 13
print (append_front_remove_rear(test_list, front))
|
OutputThe original array : [4, 5, 7, 3, 10]
[13, 4, 5, 7, 3]
Time complexity: O(n), where n is the length of the input list lst.
Auxiliary space: O(1), as the algorithm modifies the input list in place without using any additional data structures.
Method #9: Using a custom function
Define a function named “append_and_remove” that takes in a list as an argument.
In the function, append the value 13 to the front of the list using the “insert()” method.
Remove the last element from the list using the “pop()” method.
Return the modified list.
Initialize a list named “test_list” with some values.
Print the original list using the “print()” function.
Call the “append_and_remove()” function with “test_list” as an argument and store the result in a variable named “res”.
Print the modified list using the “print()” function.
Python3
def append_and_remove(lst):
lst.insert( 0 , 13 )
lst.pop()
return lst
test_list = [ 4 , 5 , 7 , 3 , 10 ]
print ( "The original list: " + str (test_list))
res = append_and_remove(test_list)
print ( "The list after append and removal: " + str (res))
|
OutputThe original list: [4, 5, 7, 3, 10]
The list after append and removal: [13, 4, 5, 7, 3]
Time complexity: O(1)
Auxiliary space: O(1)