• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Remove First Element from List

Python Remove First Element from List

February 13, 2022 Leave a Comment

In Python, we can remove the first element from a list easily – there are many ways to remove the first item from a list using Python.

The easiest way to remove the first element from a list is with slicing.

list = [1,2,9,0,1,3]

list_without_first_element = list[1:]

print(list_without_first_element)

#Output:
[2,9,0,1,3]

Another method to remove the first item in a list is with the Python pop() function.

list = [1,2,9,0,1,3]

print(list.pop(0))

#Output:
[2,9,0,1,3]

You can also use the Python del keyword to delete the first element from a list.

list = [1,2,9,0,1,3]

del list[0]

print(list)

#Output:
[2,9,0,1,3]

You can also use the Python remove() function to remove the first element of a list.

list = [1,2,9,0,1,3]

list.remove(list[0])

print(list)

#Output:
[2,9,0,1,3]

Finally, you can use the deque object from the Python collections module to get rid of the first element in a list.

from collections import deque

list = [1,2,9,0,1,3]

dequelist = deque(list)

dequelist.popleft()

print(list(dequelist))

#Output:
[2,9,0,1,3]

In Python, lists are one of the most used data structures and allow us to work with collections of data easily. When working with lists, it is useful to be able to add or remove items from the list in an easy way.

With Python, we can easily remove the first element from a list. There are multiple ways to remove the first item, and the easiest way to get rid of the first item of a list is with slicing.

To use slicing to remove the first element in a list, we start the slice at position ‘1’ and go to the end by not specifying an end to the slice.

Below is an example of how we can use slicing in Python to get rid of the first element of a list.

list = [1,2,9,0,1,3]

list_without_first_element = list[1:]

print(list_without_first_element)

#Output:
[2,9,0,1,3]

If you are looking to remove the last element from a list, you can also use slicing, or any of these methods which you will see below.

Using the pop Function in Python to Remove the First Item from a List

There are many list methods which allow us to manipulate lists in Python very easily. One such method is the Python pop() function.

The Python pop() function removes an element at the specified position. So, if we want to remove the first element, we pass ‘0’ to pop().

Below is an example of how to use the pop() function to remove the first element from a list in Python.

list = [1,2,9,0,1,3]

print(list.pop(0))

#Output:
[2,9,0,1,3]

Using the del Keyword in Python to Remove the First Element from a List

The Python del keyword allows us to delete objects. We can use the del to delete the first element of a list.

Below is an example in Python of how to use del to delete the first element of a list.

list = [1,2,9,0,1,3]

del list[0]

print(list)

#Output:
[2,9,0,1,3]

Using the remove Function in Python to Remove the First Element from a List

Another list method which is very useful to use in Python is the remove() function. The Python remove() function searches a list for a given element and removes the first match.

We can use the remove() function to get rid of the first item of a list by passing the first item to remove(). Since it’s the first element, we don’t need to worry about duplicates.

Below is an example in Python of how to use remove() to get rid of the first item of a list.

list = [1,2,9,0,1,3]

list.remove(list[0])

print(list)

#Output:
[2,9,0,1,3]

Using deque in Python to Remove the First Item from a List

The last way that you can remove the first item from a list is with the deque data structure from the Python collections module.

Deque, or doubly ended queue, is most useful if you need to quickly append or pop items from the beginning or end of your data. If you have a large collection of items, you deque can be faster than the similar list operations.

To remove the first item from a list using deque, we convert the list to deque, use the popleft() function, and then convert the result back to a list.

from collections import deque

list = [1,2,9,0,1,3]

dequelist = deque(list)

dequelist.popleft()

print(list(dequelist))

#Output:
[2,9,0,1,3]

Hopefully this article has been beneficial for you to learn the different ways you can remove the first element from a list in Python.

Other Articles You'll Also Like:

  • 1.  Create Empty File with Python
  • 2.  Using Python to Convert Float to Int
  • 3.  Using Python to Check If Variable is Not None
  • 4.  Using Python to Add Trailing Zeros to String
  • 5.  Get Days of timedelta Object in Python
  • 6.  Find All Pythagorean Triples in a Range using Python
  • 7.  Convert First Letter of String to Lowercase in Python
  • 8.  pandas to_pickle – Write DataFrame to Pickle File
  • 9.  Python getsizeof() Function – Get Size of Object
  • 10.  Python cube root – Find Cube Root of Number With math.pow() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy