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 with an index of -1. Besides these, you can also use the slicing, list comprehension, * unpacking technique, 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.
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 remove last element from list
# Initialize the list with strings
technology = [“Python”, “Spark”, “Hadoop”, “Pandas”]
# Example 1: Remove the last element from the list
# Using pop() method
last_element = technology.pop()
# Example 2: Using pop() method
last_element = technology.pop(-1)
# Example 3: Remove the last element from the list
# Using del Keyword
del technology[-1]
# Example 4: Remove the last element from the list
# Using slicing
last_element = technology[:-1]
# Example 5: Remove the last element from the list
# Using List comprehension
last_element = [x for x in technology[:-1]]
# Example 6: Remove the last element from the list
# Using the * unpacking technique
*last_element, _ = technology
# Example 7: Remove the last element from the list
# Using islice()function
last_element = list(islice(technology, len(technology)-1))
# Example 8: 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 by passing an index -1 as an argument to the pop() method. This will remove the last element from the list and return the element it removed.
In the below example first, create a list named technology containing four elements. Then called the pop() method on technology with an index of -1, which 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 with strings
technology = [“Python”, “Spark”, “Hadoop”,”Pandas”]
print(“Actual List: “,technology)
# Remove last element from list
# Using pop() method
last_element = technology.pop(-1)
print(“Final List: “,technology)
print(“Remove last element from list : “,last_element)
last_element = technology.pop()
print(“Final List: “,technology)
print(“Remove last element from list : “,last_element)
Yields below output.
3. Using del Keyword
You can also remove the last element from a list using the del keyword in Python. Like above use the index -1 to reference the last element of the list and then use del to remove it.
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.
# Initialize the list with strings
technology = [“Python”, “Spark”, “Hadoop”,”Pandas”]
print(“Actual List: “,technology)
# Remove last element from list
# Using del Keyword
del technology[-1]
print(“Final List: “,technology)
Yields below output.
4. Remove the Last Element from the List Using Slicing
You can also remove the last element from a list using the slicing technique. 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.
# Initialize the list with strings
technology = [“Python”, “Spark”, “Hadoop”,”Pandas”]
print(“Actual List: “,technology)
# Remove last element from list
# Using slicing
last_element = technology[:-1]
print(“Final List: “,last_element)
Yields the same output as above.
5. Remove the Last Element using List Comprehension
You can also remove the last element from the list using Python list comprehension. 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 of technology except the last one and creates a new list last_element with those elements. The original list technology remains unchanged.
# Initialize the list with 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(“Final List: “,last_element)
Yields the same output as above.
6. Remove the Last Element from List Using the * Unpacking Technique
You can also remove the last element from a list using the unpacking technique, you can utilize the asterisk (*) operator. For example, the unpacking technique is used to assign all but the last element of the technology list to the last_elements 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(“Final List: “,last_element)
Yields the same output as above.
7. Remove the Last Element from the List Using islice() Method
Similarly, you can also remove the last element from a list using the islice() function from the itertools module, 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(“Final List: “,last_element)
Yields the same output as above.
8. Using list.remove() Function
You can also 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(“Final List: “,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 !!
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 with an index of -1. Besides these, you can also use the slicing, list comprehension, * unpacking technique, islice(), and list.remove() function. In this article, I Read More Python, Python Tutorial, Pending review by admin, Python List Examples