Open In App

Python | Remove Front K elements

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


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)
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:

  1. Initialize the original list and print it.
  2. Initialize K and print it.
  3. Use list comprehension to create a new list by iterating through the original list starting from the Kth index.
  4. 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))


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)
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))


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 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))


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-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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads