Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Append at front and remove from rear

Python | Append at front and remove from rear

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




# Python3 code to demonstrate
# append from front and remove from rear
# using + operator and list slicing
 
# initializing list
test_list = [4, 5, 7, 3, 10]
 
# printing original list
print(& quot
       The original list : & quot
       + str(test_list))
 
# using + operator and list slicing
# append from front and remove from rear
res = [13] + test_list[:-1]
 
# printing result
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




# Python3 code to demonstrate
# append from front and remove from rear
# using collections.deque
from collections import deque
 
# initializing list
test_list = [4, 5, 7, 3, 10]
 
# printing original list
print(& quot
       The original list : & quot
       + str(test_list))
 
# using collections.deque
# append from front and remove from rear
res = deque(test_list)
res.appendleft(13)
res.pop()
res = list(res)
 
# printing result
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




# Python3 code to demonstrate
# append from front and remove from rear
 
# initializing list
test_list = [4, 5, 7, 3, 10]
 
# printing original list
print("The original list : " + str(test_list))
 
test_list.pop()
test_list.insert(0, 13)
 
# printing result
print("The list after append and removal : " + str(test_list))


Output

The 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
 
# Initialize the list
test_list = [4, 5, 7, 3, 10]
 
# Print the original list
print("The original list:", test_list)
 
# Use islice to slice the list and append/remove elements
res = [13] + list(islice(test_list, 0, len(test_list) - 1))
 
# Print the resulting list
print("The list after append and removal:", res)
# This code is contributed by Edula Vinay Kumar Reddy


Output

The 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




# Python3 code to demonstrate
# append from front and remove from rear
# using list comprehension
 
# initializing list
test_list = [4, 5, 7, 3, 10]
 
# printing original list
print("The original list : " + str(test_list))
 
res = [13] + [test_list[i] for i in range(len(test_list) - 1)]
# printing result
print("The list after append and removal : " + str(res))
 
# this code contributed by tvsk


Output

The 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
 
# initializing array
test_array = np.array([4, 5, 7, 3, 10])
 
# printing original array
print("The original array : ", test_array)
 
# using np.concatenate() and np.delete() functions
# append from front and remove from rear
res = np.concatenate(([13], np.delete(test_array, -1)))
 
# printing result
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]
# printing original array
print("The original array : ", test_list)
front = 13
print(append_front_remove_rear(test_list, front))
#This code is contributed by Jyothi pinjala.


Output

The 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




# define a function that takes a list and a new element to add to the front of the list
def append_front_remove_rear(lst, front):
    # remove the last element from the list using the pop() method
    lst.pop()
    # insert the new element at the beginning of the list using the insert() method
    lst.insert(0, front)
    # return the modified list
    return lst
     
# define a test list
test_list = [4, 5, 7, 3, 10]
# printing original array
print("The original array : ", test_list)
# define a new element to add to the front of the list
front = 13
# call the function with the test list and new element as arguments, and print the result
print(append_front_remove_rear(test_list, front))


Output

The 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




# Python3 code to demonstrate
# append from front and remove from rear
# using a custom function
 
# define function to append and remove
def append_and_remove(lst):
    lst.insert(0, 13)
    lst.pop()
    return lst
 
# initializing list
test_list = [4, 5, 7, 3, 10]
 
# printing original list
print("The original list: " + str(test_list))
 
# append from front and remove from rear using a custom function
res = append_and_remove(test_list)
 
# printing result
print("The list after append and removal: " + 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(1)
Auxiliary space: O(1)

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