The Wayback Machine - https://web.archive.org/web/20240807012439/https://www.geeksforgeeks.org/python-removing-first-element-of-list/
Open In App

Python – Remove first element of list

Last Updated : 22 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
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.



Similar Reads

Python | Subtract two list elements if element in first list is greater
Given two list, If element in first list in greater than element in second list, then subtract it, else return the element of first list only.Examples: Input: l1 = [10, 20, 30, 40, 50, 60] l2 = [60, 50, 40, 30, 20, 10] Output: [10, 20, 30, 10, 30, 50] Input: l1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] l2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output: [6, 5,
5 min read
Ways to remove particular List element in Python
List is an important container and is used almost in every code of day-day programming as well as web development. The more it is used, more is the required to master it and hence knowledge of its operations is necessary. Let's see the different ways of removing particular list elements. Method #1 : Using remove() remove() can perform the task of r
6 min read
Remove Last Element from List in Python
Given a list, the task is to write a Python program to remove the last element present in the list and update the original list in Python. Example: Input: ["geeks", "for", "geeks"]Output:["geeks", "for"] Input: [1, 2, 3, 4, 5]Output:[1, 2, 3, 4] Explanation: Here simply we have to remove the last element present in the list and print the resultant
4 min read
Remove all the occurrences of an element from a list in Python
The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Example Input1: 1 1 2 3 4 5 1 2 1 Output1: 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [2, 3, 4, 5, 2]Remove all Occurrences of an Item fr
4 min read
Python - Find first element by second in tuple List
Sometimes, while working with Python records, we can have a problem in which we need to find the first element of tuple from the given second element. This kind of problem can occur in domains such as web development. Let's discuss certain ways in which this task can be performed. Input : test_list = [(4, 5), (5, 6), (1, 3), (6, 6)] K = 6 Output :
4 min read
Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
Python | Insert Nth element to Kth element in other list
Sometimes, while working with Python list, there can be a problem in which we need to perform the inter-list shifts of elements. Having a solution to this problem is always very useful. Let’s discuss the certain way in which this task can be performed. Method 1: Using pop() + insert() + index() This particular task can be performed using a combinat
9 min read
How Did Facebook Remove 2.2 Billion Fake Accounts in the First Quarter of 2019?
Ever heard of Facebook? Well, unless you have been living under a rock this past decade, I'm sure you have. And in case you haven't, Facebook is a social networking site that is used to share pictures, post comments on the aforementioned pictures, watch news (only if extremely bored!), chat live with people, watch short-form videos, etc. (Although
4 min read
Python - Remove last element from dictionary
Given a dictionary, the task is to remove the last occurring element, i.e. the last key-value pair in the dictionary. Example: Input: {1: 'Geeks', 2: 'For', 3: 'Geeks'} Output:{1: 'Geeks', 2: 'For'}Method 1: Using popitem() method Python dictionary popitem() method removes one key, value pair as a two-item tuple and modifies the removes the last ke
5 min read
Python Remove Duplicates from a List
The job is simple. We need to take a list, with duplicate elements in it and generate another list that only contains the element without the duplicates in them. Examples: Input : [2, 4, 10, 20, 5, 2, 20, 4] Output : [2, 4, 10, 20, 5] Input : [28, 42, 28, 16, 90, 42, 42, 28] Output : [28, 42, 16, 90] We can use not in on list to find out the duplic
3 min read
Practice Tags :
three90RightbarBannerImg