You are currently viewing Python List Remove Last Element

How to remove the last element/item from a list in Python? To remove the last element from the list, you can use the del keyword or the pop() method. Besides these, you can also use slicing, list comprehension, * unpacking techniques, islice(), and list.remove() function. In this article, I will explain how to remove the last element from a list by using all these methods with examples.

Advertisements

Related: In Python, you can remove the first element from a list.

1. Quick Examples of Removing the Last Element from the List

If you are in a hurry, below are some quick examples of how to remove the last element from a list.


# Quick examples of removing last element from list

# Initialize the list of strings
technology = ["Python", "Spark", "Hadoop", "Pandas"]

# Example 1: Remove the last element from the list 
# Using pop() method
last_element = technology.pop()

# Example 2: Remove the last element from the list 
# Using del Keyword
del technology[-1]

# Example 3: Remove the last element from the list 
# Using slicing
last_element = technology[:-1]

# Example 4: Remove the last element from the list 
# Using List comprehension
last_element = [x for x in technology[:-1]]

# Example 5: Remove the last element from the list 
# Using the * unpacking technique
*last_element, _ = technology

# Example 6: Remove the last element from the list 
# Using islice()function
last_element = list(islice(technology, len(technology)-1))

# Example 7: Using list.remove() function
technology.remove("Pandas")

2. Remove the Last Element from the List Using pop()

You can remove the last element from a list using the pop() method in Python. This method by default can remove the last element from the list and return the removed element.

In the below example first, create a list technology containing four elements. Then use the pop() method on technology that removes the last element ‘Pandas’ from the list and returns it. Assign the returned value to a variable last_element and print both technology and last_element to verify that the last element has been removed from the list.


# Initialize the list of strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Remove last element from list 
# Using pop() method
last_element = technology.pop()
print("The list after removing last element: ",technology)
print("Removed element : ",last_element)

Yields below output.

python list remove last element

3. Using del Keyword

To remove the last element from a list you can use the del keyword in Python. Using this keyword you can delete the whole list. If you want to remove a specific element from a list you can go with the corresponding reference.

Here, first, create a list called technology containing four elements. Then used the del keyword to remove the last element from the list by referencing it with the index -1.

Related: In Python, you can use negative indexing to access and remove the elements of a list.


# Initialize the list of strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Remove last element from list 
# Using del Keyword
del technology[-1]
print("The list after removing last element: ",technology)

Yields below output.

python list remove last element

4. Remove the Last Element from the List Using Slicing

You can also use slicing technique to move the last element from a list. Use the slicing technique to select a particular portion of a list. However, slicing the list as technology[:-1] will create a new list containing all elements except the last one, rather than modifying the original list. For example, the slice [:-1] creates a new list, last_element, containing all elements from technology except the last one. The original list, technology, remains unchanged.

Related: You can also get the last N elements from a list.


# Initialize the list of strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Remove last element from list 
# Using slicing
last_element = technology[:-1]
print("The list after removing last element: ",technology)

Yields the same output as above.

5. Remove the Last Element using List Comprehension

Alternatively, you can use Python list comprehension to remove the last element from the list. The list comprehension [x for x in technology[:-1]] is used to create a new list named last_element that contains all the elements of the technology list except for the last one.

In the below example, the list comprehension [x for x in technology[:-1]] iterates over all elements technology except the last one and creates a new list last_element with those elements. The original list technology remains unchanged.

Related: You can also remove the first element from a list in Python.


# Initialize the list of strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Remove last element from list 
# Using List comprehension
last_element = [x for x in technology[:-1]]
print("The list after removing last element: ",technology)

Yields the same output as above.

6. Remove the Last Element from the List Using the * Unpacking Technique

You can also use unpacking(*) operator to remove the last element from a list. For example, the unpacking technique is used to assign all but the last element of the technology list to the last_element variable. The underscore (_) is used to discard the last element, as it is not needed in this case.


# Initialize the list with strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Remove last element from list 
# Using the * unpacking technique
*last_element, _ = technology
print("The list after removing last element: ",technology)

Yields the same output as above.

7. Remove the Last Element from the List Using islice() Method

Similarly, you can use islice() function from the itertools module, to remove the last element from a list. First, you would need to convert the list to an iterator and then slice it. For example, islice() is used to slice the list iterator technology up to the second-to-last element. The len(technology)-1 argument specifies the stopping point for the slice. Converting the result to a list last_elements will contain all elements of the list except the last one.


from itertools import islice

# Initialize the list with strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Remove last element from list 
# Using islice()function
last_element = list(islice(technology, len(technology)-1))
print("The list after removing last element: ",technology)

Yields the same output as above.

8. Using list.remove() Function

Finally, you can use the list.remove() function to remove a specific element from a list. For example, the list.remove() function is used to remove the element "Pandas" from the list. The function searches for the first occurrence of the specified value and removes it from the list. After removing the element, the list is updated, and the modified list is printed.


# Initialize the list with strings
technology = ["Python", "Spark", "Hadoop","Pandas"]
print("Actual List: ",technology)

# Using list.remove() function
technology.remove("Pandas")
print("The list after removing last element: ",technology)

Yields the same output as above.

9. Conclusion

In this article, I have explained how to remove the last element from a list in Python by using the pop(), del keyword, slicing, list comprehension, * unpacking technique, islice(), and list.remove() function with examples.

Happy Learning !!