Python | Remove Front K elements
Last Updated :
15 May, 2023
We often come to situations in which we need to decrease the size of the list by truncating the first 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 last len(list) – K elements to be in the list and hence removing the first K elements.
Python3
# Python code to demonstrate
# Remove Front 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 Front K elements
res = test_list[K:]
# printing result
print ("The list after removing first K elements : " + str(res))
Output : The original list is : [1, 4, 6, 3, 5, 8]
The list after removing first K elements : [3, 5, 8]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The len() + list slicing is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2: Using Negative list slicing
We can perform this particular task using negative list slicing in which we start removing the elements from the first index of the list and hence remove all the K elements from the first. We remove None if 0 elements are asked to be removed.
Python3
# Python code to demonstrate
# Remove Front 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 Front K elements
res = test_list[-(len(test_list) - K) or None:]
# printing result
print("The list after removing first K elements : " + str(res))
Output : The original list is : [1, 4, 6, 3, 5, 8]
The list after removing first K elements : [3, 5, 8]
Time complexity: O(1) because the res list is generated in constant time regardless of the length of the input list.
Auxiliary space: O(N) because a new list res is created that contains N-K elements from the original list test_list.
Method #3: Using islice()
We can use the itertools.islice() function to remove the first K elements from the list. The function takes an iterable, a starting index, and an ending index, and returns an iterator that contains all the elements from the iterable between the starting and ending index. In this case, we can use the starting index as K and the ending index as the length of the list, effectively removing the first K elements from the list.
Python3
# Python code to demonstrate
# Remove Front K elements
# using islice()
from itertools import islice
# 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 islice()
# Remove Front K elements
res = list(islice(test_list, K, len(test_list)))
# printing result
print("The list after removing first K elements : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : [1, 4, 6, 3, 5, 8]
The list after removing first K elements : [3, 5, 8]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list comprehension
In this method, we can create a new list by iterating through the original list starting from the Kth index and appending each element to the new list.
Steps:
- Initialize the original list and print it.
- Initialize K and print it.
- Use list comprehension to create a new list by iterating through the original list starting from the Kth index.
- Print the new list.
Python3
# Python code to demonstrate
# Remove Front K elements
# using list comprehension
# 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 list comprehension
# Remove Front K elements
res = [test_list[i] for i in range(K, len(test_list))]
# printing result
print("The list after removing first K elements : " + str(res))
OutputThe original list is : [1, 4, 6, 3, 5, 8]
The list after removing first K elements : [3, 5, 8]
Time complexity: O(n)
Auxiliary space: O(n)
Method #5: Using a for loop
Step-by-step approach:
- Initialize the value of K to the number of elements to remove from the front of the list.
- Initialize an empty list res to store the resulting list after removing the first K elements.
- Use a for loop to iterate through the elements of test_list starting from index K to the end of the list.
- Append each element from test_list starting from index K to the end of the list to the res list.
- Print the resulting list res to verify the elements removed from the front of the original list.
Below is the implementation of the above approach:
Python3
# Python code to demonstrate
# Remove Front K elements
# using a for loop
# 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 a for loop
# Remove Front K elements
res = []
for i in range(K, len(test_list)):
res.append(test_list[i])
# printing result
print("The list after removing first K elements : " + str(res))
OutputThe original list is : [1, 4, 6, 3, 5, 8]
The list after removing first K elements : [3, 5, 8]
Time complexity: O(N), where N is the number of elements in the original list test_list.
Auxiliary space: O(N), where N is the number of elements in the original list test_list.
Method #6: Using the del keyword
Step-by-step approach
- Initialize the list test_list with some values.
- Print the original list using print().
- Initialize K to the number of elements to remove from the front of the list.
- Use the del keyword to delete the first K elements of the list.
- Print the resulting list using print().
Python3
# Python code to demonstrate
# Remove Front K elements
# using the del keyword
# 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 the del keyword
# Remove Front K elements
del test_list[:K]
# printing result
print("The list after removing first K elements : " + str(test_list))
OutputThe original list is : [1, 4, 6, 3, 5, 8]
The list after removing first K elements : [3, 5, 8]
Time complexity: O(n-k), where n is the length of the list and k is the number of elements to remove from the front of the list.
Auxiliary space: O(1), since no additional space is used other than the original list.
Similar Reads
Python - Remove rear element from list
A stack data structure is a very well-known data structure, lists in Python usually append the elements to the end of the list. For implementing a stack data structure, it is essential to be able to remove the end element from a list. Letâs discuss the ways to achieve this so that stack data structu
6 min read
Python | Remove Initial K column elements
Sometimes, while working with Matrix data, we can have stray elements that attached at front end of each row of matrix. This can be undesired at times and wished to be removed. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + del + list slicing The combination
4 min read
Python | Retain K Front and Rear elements
Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element retention and deletion of rest elements. It is a good utility whose solution can be useful to have. Letâs discuss certain ways in
8 min read
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 min read
Python - Remove non-increasing elements
Given a list, our task is to write a Python program to remove all the non-increasing elements from the list. Input : test_list = [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1] Output : [5, 5, 5, 7, 9, 10, 10, 12, 13, 16] Explanation : 3, 4 are omitted as 5, (greater element) had occurred before
3 min read
Python - First K unique elements
Sometimes, while working with Python Lists, we can have a problem in which we need to extract first K unique elements. This means we need to extract duplicate if they occur in first K elements as well. This can essentially make count of first K unique elements more than K. This kind of problem can h
3 min read
Python - Odd elements removal in List
Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
5 min read
Python | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Remove last K elements of list - Python
Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python.Using list slicingList slicing is one of
3 min read
Remove Negative Elements in List-Python
The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
3 min read