Python

Python Pop Last Element from List

Lists” are changeable data structures that can store/keep numerous values in one/single variable. Also, we can change the contents of a list easily. Python is a popular programming language that uses lists as one of its built-in features to apply various functionalities in accordance with the requirement. In this post, we will focus on the approaches to withdraw the last element from a list.

What is the Python List?

The Python “list” is composed of elements/items that are ordered and can be modified. To define a list, put a comma-separated sequence of elements/items in “square brackets[ ]”. The elements of a list can be of various data types such as int, str, or even other lists. Here is a Python list example:

list1 = ['Joseph', 'Lily', 'Anna', 'Jon']

 
The elements of a list are accessed by referring to their index. In a list, the first element has an index of “0“.

How to Pop the Last Item/Element From a Python List?

Python provides several approaches to remove/eliminate the last element from a list. Some of the approaches are listed below:

Method 1: Remove/Pop the Last Elements From a List Utilizing the “pop()” Method

The “pop()” method is a built-in function in Python that eliminates and retrieves the last element of a list.

Syntax

list_name.pop()

 
Example 1: Remove the Last Element From the List

The code given below is used to delete the last list element:

list1 = ['Joseph', 'Lily', 'Anna', 'Jon']
print('Original List: ', list1)
list1.pop()
print('\nList After Removing Last Element: ', list1)

 
In the above code, the “pop()” method removes the last element “Jon” from the created list named “list1” and returns the updated list.

Output


This output implies that the last element has been sacked from the input list.

Example 2: Remove/Delete Specific Element/Item From the List

The “pop()” method can also be utilized to terminate and retrieve an element at a particular index.

Syntax

list_name.pop(index)

 
Here, “index” refers to the element’s index in the list.

The following code snippet is employed to terminate the specific element of the list via indexing:

list1 = ['Joseph', 'Lily', 'Anna', 'Jon']
print('Original List: ', list1)
list1.pop(1)
print('\nList After Removing Specific Element: ', list1)

 
In the above code block, the “pop()” method removes the element at index 1 i.e., “Lily” from the given list and returns the updated list.

Output


The output snippet implies that the item “Lily” has been deleted from the input list.

Method 2: Remove/Pop the Last Elements From a List Utilizing the “Slicing” Method

The Python “Slicing” can also be utilized to remove the last element from an input list. “Slicing” is a technique that authorizes you to extract a part of the list.

Syntax

list_name[start:end]

 
Example

Here’s an example of using “slicing” to remove the list last element:

list1 = ['Joseph', 'Lily', 'Anna', 'David']
print('Original List: ', list1)
output = list1[:-1]
print('\nList After Removing Last Element: ', output)

 
Based on the example code, “slicing” is used to remove the last element from the given list. It is such that the “list1[:-1]” syntax extracts all elements of the list except the last one, thereby effectively removing the last list element and returning the rest of the elements.

Output


As analyzed, the last element has been terminated from the original list appropriately.

Method 3: Remove/Pop the Last Elements From a List Utilizing the “del” Statement

The “del” statement is also utilized to delete the last list elements in Python.

Syntax

del list_name[index]

 
In this syntax, “index” points to the element index that needs to be removed.

Example

Here is an example of how the “del” statement can be used to remove the last element from a list:

list1 = ['Joseph', 'Lily', 'Anna', 'David']
print('Original List: ', list1)
del list1[-1]
print('\nList After Removing Last Element: ', list1)

 
According to the above code block, the “del” statement is used to remove the last element from the created list. The “list1[-1]” syntax refers to the last element of the list, which is then deleted using the “del” statement.

Output


In this outcome, the last element has been removed successfully.

Conclusion

The “pop()” method, “Slicing” method, or the “del” statement is used in Python to remove/pop the last element or specific element from the given list. The “pop()” method clears the last element from the list directly and removes the specific element as well via indexing. Similarly, the “Slicing” method and “del” statement can also be used to remove the specific element from the list. This Python blog proposed various ways to pop the last element from the list.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.