• 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 / Remove Last Element from List Using Python

Remove Last Element from List Using Python

February 13, 2022 Leave a Comment

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

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

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

list_without_last_element = list[:-1]

print(list_without_last_element)

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

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

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

print(list.pop())

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

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

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

del list[-1]

print(list)

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

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

from collections import deque

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

dequelist = deque(list)

dequelist.pop()

print(list(dequelist))

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

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 last element from a list. There are multiple ways to remove the last item, and the easiest way to get rid of the last item of a list is with slicing.

To use slicing to remove the last element in a list, we don’t provide a start position and then specify ‘-1’ as the end position to get everything except the last element.

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

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

list_without_last_element = list[:-1]

print(list_without_last_element)

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

If you are looking to remove the first 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 Last 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. If no position is specified, then pop() removes the last element in the list.

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

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

print(list.pop())

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

Using the del Keyword in Python to Remove the Last Element in a List

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

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

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

del list[-1]

print(list)

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

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

The last way that you can remove the last 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 last item from a list using deque, we convert the list to deque, use the pop() 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.pop()

print(list(dequelist))

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

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

Other Articles You'll Also Like:

  • 1.  pandas Standard Deviation – Using std() to Find Standard Deviation
  • 2.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 3.  Python isdigit() Function – Check if Characters in String are Digits
  • 4.  Drop Duplicates pandas – Remove Duplicate Rows in DataFrame
  • 5.  Format Numbers as Dollars in Python with format()
  • 6.  pandas drop – Drop Rows or Columns from DataFrame
  • 7.  Get Current Year in Python
  • 8.  Using Python to Count Number of False in List
  • 9.  Check if String Contains Numbers in Python
  • 10.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame

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