Code Example: Remove The First Item From List Python

Are you looking to remove the first item from List? In this article, you will learn how to remove the first element from the List using Python with code examples.

In Python you can remove the first element from the List using many inbuilt functions such as pop(), remove(), and del. These functions and operators allow you to delete the first item from the list. Now let us see the code example for all the above methods.

1. Remove The First Item From List Using pop() Method

In the Python pop() method remove the first element from the list. All you are required is to provide the index of the first element as an input argument to the pop function and it will delete the first element from the list.

Let us see in the below example code the usage of the pop[1] method to remove the first element from the list using Python.

#Python Code Example

#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Using Pop removing the first item 
#From the Above List
givenList.pop(0)

#Printing the List with No First Element
print(givenList)

Output:

['You', 'Are', 'Visiting', 'Coduber']

As you can see using the above code I was able to remove the first element from the list that is ‘com’. So, using pop with index as 0 for the first element you can easily remove the first element from the list in Python.

2. Remove First Item from the List using remove() Method in Python

The second remove() method that you can use to remove the first item from the list is in Python. This method takes the value as an input and hence you need to provide the value at first index from the given list as an input argument and this method will remove that item from the list.

Let us see in the below example code the usage of the remove() method to remove the first element from the list in Python.

#Python Code Example

#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Using remove() for removing the first item 
#From the Above List
givenList.remove(givenList[0])

#Printing the List with No First Element
print(givenList)

Output


['You', 'Are', 'Visiting', 'Coduber']

As you can see in the above code, I provided the first element as an input argument to the remove function and it removed the first item from the list. Though remove method can be used to remove any from the list if you know the value that you want to remove.

3. Remove First Item from The List Using del Statement in Python

In Python, there is a del statement present inside the data structure class. This statement lets you delete any element from the list given the value as an input argument. The del[2] statement can be used to remove multiple things including the slices or the entire list element.

Let us see in the below example code the usage of the del statement to remove the first element from the list.

#Python Code Example

#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Using del statement for removing 
# the first item From the Above List
del givenList[0]

#Printing the List with No First Element
print(givenList)

Output:

['You', 'Are', 'Visiting', 'Coduber']

As you can see from the above code, using the del statement with the first index value of the list I was able to remove the first item from the list. Similarly, you can remove any element from the list using this del statement it is not limited to the first element.

4. Using popleft() To Remove First Item from the List in Python

If you are having a lot of lists from which you need to remove the first element then all the above methods discussed will hit your performance poorly, and hence to have a higher performance you should use the popleft() method from the collections library of Python.

Let us see in the below example code the usage of popleft() function to achieve higher performance goal to remove the first item from the list in Python.

#Python Code Example

#importing collections library
from collections import deque

#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Using popleft function for removing 
# the first item From the Above List
getDequequed = deque(givenList)
getDequequed.popleft()

#Assigning the Latest List
givenList = list(getDequequed)

#Printing the List with No First Element
print(givenList)

Output:

['You', 'Are', 'Visiting', 'Coduber']

As you can see from the above code, the first element was removed using the popleft() function. But to do that you need to assign the list to deque and then use the popleft() method to remove the first item successfully.

5. Using The Slicing Method to Remove the First Item from the List in Python

The fifth method that you can use to remove the first item from the list is using the list slicing method. Slicing is a better method to use instead of the pop() method as if there is no element in the list pop method can throw an error while slicing will not do anything.

Let us see in the below example the usage of the slicing method to remove the first element using Python.

#Python Code Example

#importing collections library
from collections import deque

#Initializing the List
givenList = ['com', 'You', 'Are', 'Visiting', 'Coduber']

#Using slicing method for removing 
# the first item From the Above List
givenList = givenList[1:]

#Printing the List with No First Element
print(givenList)

Output:

['You', 'Are', 'Visiting', 'Coduber']

As you can see from the above code using slicing I was able to remove the first element. And you can again use slicing to remove any number to element either from starting of the list or from the ending of the list.

Code Example: Remove The First Item From List Python

Wrap Up

I hope you were able to learn how to remove the first item from the list in Python. I listed down the five methods above that you can use to solve your problem and each method have its own advantages if you want performance then use the fourth method, else all other methods will be the same as per performance.

Let me know in the comment section if you know any better method than the one discussed above, I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. 4 Ways To Loop Through List With Index Using Python
  2. Code Examples: Multiply In Python
  3. How To Join Words In a List Using Python Code Example
  4. Python Code Example: How To Find Element in a List in 6 Way
  5. How To Test For An Empty Object In JavaScript
  6. How To Iterate Over Rows in a DataFrame in Pandas Python

Leave a Comment