Python | Remove last K elements of list
We often come to the situations in which we need to decrease the size of the list by truncating the k last elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar size. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using len() + list slicing
List slicing can perform this particular task in which we just slice the first len(list) – K elements to be in the list and hence removing the last K elements.
# Python code to demonstrate # remove last K elements # using len() + list slicing # initializing list test_list = [1, 4, 6, 3, 5, 8] # printing original list print ("The original list is : " + str(test_list)) # initializing K K = 3 # using len() + list slicing # remove last K elements res = test_list[: len(test_list) - K] # printing result print ("The list after removing last K elements : " + str(res)) |
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]
Method #2 : Using Negative list slicing
We can perform this particular task using the negative list slicing in which we start removing the elements from the last index of the list and hence remove all the K elements from the last. We remove None if 0 elements are asked to be removed.
# Python code to demonstrate # remove last K elements # using negative list slicing # initializing list test_list = [1, 4, 6, 3, 5, 8] # printing original list print ("The original list is : " + str(test_list)) # initializing K K = 3 # using negative list slicing # remove last K elements res = test_list[: -K or None] # printing result print ("The list after removing last K elements : " + str(res)) |
The original list is : [1, 4, 6, 3, 5, 8] The list after removing last K elements : [1, 4, 6]
Recommended Posts:
- Remove multiple elements from a list in Python
- Python | Remove elements of list that are repeated less than k times
- Python | Remove unordered duplicate elements from a list
- Python | Remove trailing empty elements from given list
- Python | Remove all values from a list present in other list
- Python list remove()
- Python | Remove Duplicates from a List
- Python | Remove None values from list
- Python | Remove given element from the list
- Python | Remove all occurrences in nested list
- Python | Remove repeated sublists from given list
- Python | Remove duplicates from nested list
- Python | Remove empty tuples from a list
- Python | Ways to remove duplicates from list
- Python | Remove all strings from a list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



