Python Remove First Element from List

Python remove first element from list | We are using pop(), remove(), del statement, slice operator, numpy array, and deque() + popleft() method to remove first element from the list. If the original list is [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]. The list after removing first element [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’].

Python Remove First Occurrence from List

Python list method remove() searches for the given index element in the list and removes that element from the list. The method does not return any value but removes the given object from the list. Syntax: list_name.remove(list_name[0])

# Python program to remove first element from list

# take list
my_list = ['C', 'Python', 'Java', 'Javascript', 'Know Program']

# printing original list
print('List:', my_list)

# removed first element using remove()
my_list.remove(my_list[0])

# print list after first item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]
New list: [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]

Python List pop First

Here, we will remove the first element using the pop() function. pop() is an inbuilt function in Python that removes the element at the given index from the list. Syntax: list_name.pop(index)

# Python program to remove first element from list

# take list
my_list = ['C', 'Python', 'Java', 'Javascript', 'Know Program']

# printing original list
print('List:', my_list)

# removed first element using pop()
my_list.pop(0)

# print list after first item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]
New list: [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]

Note:- If the list is empty as it tries to pop from an empty list then, the pop() function raises an IndexError.

Using del Statement

We will delete the first element using the del operator. The del operator removes the item or an element at the specified index location from the list, but the removed item is not returned, as it is with the pop() method. So essentially, this operator takes the item’s index to be removed as the argument and deletes the item at that index. Syntax: del list_name[index]

# Python program to remove first element from list

# take list
my_list = ['C', 'Python', 'Java', 'Javascript', 'Know Program']

# printing original list
print('List:', my_list)

# removed first element using del statement
del my_list[0]

# print list after first item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]
New list: [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]

Using Slice Operator

We will remove the first element from the given list using slicing. By slicing it from the 1 index, we have removed the first element from the list. The my_list[1:] specifies all the elements of the list except the first one.

# Python program to remove first element from list

# take list
my_list = ['C', 'Python', 'Java', 'Javascript', 'Know Program']

# printing original list
print('List:', my_list)

# removed first element using slicing
new_list = my_list[1:]

# print list after first item deletion
print('New list:', new_list)

Output:-

List: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]
New list: [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]

Using NumPy Operator

This python program performs the same task but in a different way. NumPy arrays are technically also arrays, and since they are commonly used (especially in machine learning), let’s show one of the ways to remove an element from a NumPy array. The delete() is a static method declared in the NumPy module. It accepts the array and the index of the element to remove. This method returns a new array without the removed element.

# Python program to remove first element from list
import numpy as np

# take list
my_list = ['C', 'Python', 'Java', 'Javascript', 'Know Program']

# printing original list
print('List:', my_list)

# removed first element using numpy
new_list = np.delete(my_list, 0)

# print list after first item deletion
print('New list:', new_list)

Output:-

List: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]
New list: [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]

Using deque() + popleft()

Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Converting the list into deque and then performing the popleft, removes the element from the front of the list.

deque.popleft() is faster than list.pop(0), because the deque has been optimized to do popleft() approximately in O(1), while list.pop(0) takes O(n)

# Python program to remove first element from list
from collections import deque

# take list
my_list = ['C', 'Python', 'Java', 'Javascript', 'Know Program']

# printing original list
print('List:', my_list)

# removed first element using deque() + popleft()
new_list = deque(my_list)
new_list.popleft()

# print list after first item deletion
print('New list:', list(new_list))

Output:-

List: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]
New list: [‘Python’, ‘Java’, ‘Javascript’, ‘Know Program’]

Also See:- Remove the Last Element from List in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *