Remove last element from list Python
In this post, you will see how to remove the last element from a list in Python.
A list is a sequence of indexed and ordered values, like an array. It is mutable, which means we can change the order of elements in a list. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. Each element of the list has an index, starting with 0, 1, and so on. In the development process, we may come to a situation in which we need to decrease the size of the list by removing the last k elements of the list.
In Python, there are numerous methods available for the list data type that help you eliminate an element from a given list.
Remove last element from list using pop()
In Python, the list class provides a function pop(index). The Python pop() method removes an item at a specified index value from a list. The syntax of the pop() method is:
list.pop(index)
The pop() function takes in an index value, then checks whether the item exists in the list. If the item does exist, the method will remove the item from the list and return the item it has removed. If the item does not exist, an error will be returned.
Here, the index is optional that specifies the position of the element you want to remove. By default, it removes the last element from the list. Here is the example-
list_elems = [43, 45, 89, 29, 30, 63, 10]
# Remove last element from list
list_elems.pop()
print(list_elems)
Output of the above code-
[43, 45, 89, 29, 30, 63]
Remove last element from list using del statement
In Python, del is a keyword. We can use this del keyword to delete any object, like lists, variables, user-defined objects, items within lists, dictionaries, and so on. The syntax of the del is-
del obj_name
To delete the last element from a list, apply negative indexing to the list, and offer it to the del keyword to erase it.
list_elems = [21, 65, 38, 12, 22, 31, 19]
# Remove last element from list
del list_elems[-1]
print(list_elems)
Output of the above code-
[21, 65, 38, 12, 22, 31]
Remove last element from list using slicing
We can also use slicing to remove the last element from the list. In this, we can get a part of it using the Python slicing operator. The syntax of slicing is-
[start : stop : steps]
It means the slicing starts from the start index and goes up to stop in a step of steps. Here is the example-
list_elems = [30, 21, 19, 11, 52, 71, 20]
# Remove last element from list
print(list_elems[:-1])
Output of the above code-
[30, 21, 19, 11, 52, 71]
Related Articles
Python program to sum all the numbers in a list
Difference between tuple and list in Python
Alphabetical order Python
Python program to read two numbers and print their quotient and remainder
ASCII value in Python
Greatest common divisor (GCD) in Python
Python nonlocal keyword
Prime factor Python
casefold in Python
strip function in Python
Convert array to list Python
Remove element from list Python
Convert list to dictionary Python
Python dict inside list
Convert list to string Python
Remove last element from list Python
Convert string to list Python
Difference between tuple and list in Python
Alphabetical order Python