Open In App
Related Articles

Python – Remove first element of list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The queue data structure is a very well-known data structure, lists in Python usually append the elements to the end of the Python list. For implementing a queue data structure, it is essential to be able to remove the front element from a list. Let’s discuss the ways of removing the first element of the list.

Method 1: Remove Elements From Lists in Python using pop() 

This pop() method, i.e removes and prints the ith element from the list. This method is mostly used among the other available options to perform this task. This changes the original list. 

Python3

# initializing list
test_list = [1, 4, 3, 6, 7]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# using pop(0) to perform removal
test_list.pop(0)
 
# Printing modified list
print("Modified list is : " + str(test_list))

                    

Output :

Original list is : [1, 4, 3, 6, 7]
Modified list is : [4, 3, 6, 7]

Method 2: Remove Elements From Lists in Python using del list[] 

This is just the alternate method to perform the front deletion, this del operator also performs the removal of list elements in place and decreases the size of the list by 1. 

Python3

# initializing list
test_list = [1, 4, 3, 6, 7]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# using del list[0] to perform removal
del test_list[0]
 
# Printing modified list
print("Modified list is : " + str(test_list))

                    

Output :

Original list is : [1, 4, 3, 6, 7]
Modified list is : [4, 3, 6, 7]

Method 3: Remove Elements From Lists in Python using Slicing

Python Slicing is another approach by which this problem can be solved, we can slice the list from the second element to the last and assign it to the empty list. This does not do the in-place conversion as in the case of the above two methods. 

Python3

# initializing list
test_list = [1, 4, 3, 6, 7]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# using slicing to perform removal
res = test_list[1:]
 
# Printing modified list
print("Modified list is : " + str(res))

                    

Output :

Original list is : [1, 4, 3, 6, 7]
Modified list is : [4, 3, 6, 7]

Method 4: Remove Elements From Lists in Python using deque() + popleft()

This is a lesser-known method to achieve this particular task, converting the list into deque and then performing the pop left, removes the element from the front of the list. 

Python3

from collections import deque
 
# initializing list
test_list = [1, 4, 3, 6, 7]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# using deque() + popleft() to
# perform removal
res = deque(test_list)
res.popleft()
 
# Printing modified list
print("Modified list is : " + str(list(res)))

                    

Output :

Original list is : [1, 4, 3, 6, 7]
Modified list is : [4, 3, 6, 7]

Method 5: Remove Elements From Lists in Python using remove()

The remove() function allows you to remove the first instance of a specified value from the list. This can be used to remove the list’s top item. Pick the first member from the list and feed it to the remove() function.

Python3

test_list = [1, 4, 3, 6, 7, 10]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# Remove first element from list in python
test_list.remove(test_list[0])
 
# Printing modified list
print("Modified list is : " + str(test_list))

                    

Output :

Original list is : [1, 4, 3, 6, 7, 10]
Modified list is : [4, 3, 6, 7, 10]

Method:6 using List comprehension

 step-by-step algorithm for implementing the approach 

  1. A list test_list
  2. Initialize an empty list new_list
  3. Iterate over the elements in test_list starting from the second element:
    a. Append the current element to new_list
  4. Return new_list

Python3

# initializing list
test_list = [1, 4, 3, 6, 7]
 
# remove first element using list comprehension
new_list = [x for x in test_list[1:]]
 
# print the new list
print("Modified list is : " + str(new_list))

                    

Output
Modified list is : [4, 3, 6, 7]

Time complexity:

The time complexity of this approach is O(n), where n is the number of elements in the input list test_list.
This is because the algorithm iterates over each element in the list once, except for the first element, which is skipped.
Auxiliary space:

The auxiliary space complexity of this approach is O(n), where n is the number of elements in the input list test_list.
This is because the algorithm creates a new list new_list to store the modified version of the input list, which could potentially have the same number of elements as test_list.

Using numpy.delete():

  1. Import the numpy library
  2. Create a numpy array from the list to be modified
  3. Use the numpy.delete() function to remove the first element of the array
  4. Convert the modified numpy array back to a list
  5. Print the original and modified lists

Python3

import numpy as np
 
# initializing list
test_list = [1, 4, 3, 6, 7]
 
# Printing original list
print("Original list is : " + str(test_list))
 
# Convert list to numpy array
np_array = np.array(test_list)
 
# Use numpy.delete() to remove first element
modified_np_array = np.delete(np_array, 0)
 
# Convert numpy array back to list
modified_list = modified_np_array.tolist()
 
# Printing modified list
print("Modified list is : " + str(modified_list))

                    

Output:

Original list is : [1, 4, 3, 6, 7]
Modified list is : [4, 3, 6, 7]

Time complexity:
The time complexity of the numpy.delete() function is O(n), where n is the number of elements in the numpy array. Since we also convert the list to a numpy array and back to a list, the overall time complexity of this approach is O(n).

Space complexity:
The space complexity of this approach is O(n), where n is the number of elements in the input list test_list. This is due to the creation of the numpy array.



Last Updated : 22 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads