Lists are one of the most commonly used data structures in python. In python, we have different methods to perform operations on lists. In this article, we will look at different ways to remove the first element from a list using different methods like slicing, pop() method, remove() method and del operation.
Remove first element from list using slicing
In python, slicing is an operation to create a subpart of a string or a list. With slicing, we can access different parts of any string, tuple or a list. To perform slicing on a list named myList, we use the syntax myList[start, end, difference]
where “start” and “end” are the indices at which the sliced list starts and ends in the original list respectively. The “difference” is used to select elements in a sequence. The elements are selected from the list at the index= “start+ n*difference” where n is a whole number and index should be less than “end”.
To use slicing for removing the first element of the list, we will take out the sub list starting from the second element and we will leave behind the first element. Thus the first element will be removed from the list. This can be done as follows.
myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
myList = myList[1::1]
print("List after modification is:", myList)
Output:
Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]
Remove first element from a list using pop() method
The pop() method is used to remove any element from the list from a specified index. It takes the index of the element as an optional input parameter and returns the element at the specified index after deleting it from the list. If no input parameter is passed, it will return the last element of the list after deleting it.
To delete the first element of the list using the pop() method, we will invoke the pop() method on the list and will pass an index of the first element, i.e. 0 as input. It will return the first element of the list after deleting it from the list as follows.
myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
myList.pop(0)
print("List after modification is:", myList)
Output:
Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]
Using remove() method
he remove() method is used to remove the first occurrence of any element in the list. The remove() method, when invoked, takes the element to be deleted from the list as input and deletes the element from the list. When the element is not present in the list, it raises ValueError. To handle the ValueError, you can use exception handling using python try except blocks.
To delete the first element of the list using the remove() method, we will access the first element of the list using its index i.e. 0. Then we will pass the element as a parameter to the remove() method. This will delete the first element of the list as follows.
myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
element = myList[0]
myList.remove(element)
print("List after modification is:", myList)
Output:
Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]
Remove first element from a list using del keyword
The del keyword in python is used to delete objects. In python, every variable points to an object. As we know that the first element of the list points to an object in the memory, we can also delete it using del keyword as follows.
myList = [1, 2, 3, 4, 5, 6, 7]
print("Original List is:", myList)
del myList[0]
print("List after modification is:", myList)
Output:
Original List is: [1, 2, 3, 4, 5, 6, 7]
List after modification is: [2, 3, 4, 5, 6, 7]
Conclusion
In this article, we have seen different ways to delete the first element from a list in python. We have used slicing, pop() method, remove() method and del keyword to delete the first element from the list. To learn more about lists, you can read this article on list comprehension. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.