Python | Truncate a list
Last Updated :
26 Apr, 2023
Sometimes there is a requirement to restrict the list size to a particular number and remove all the elements from list which occur after a certain number as decided as list size. This is a useful utility for web development using Python. Let’s discuss certain ways in which this can be performed.
Method #1 : Using pop() pop function can be repeated a number of times till size of the list reaches the threshold required as the size of the list. This uses a whole loop and hence is comparatively tedious.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 3 , 8 , 9 , 10 ]
print ("The original list is : " + str (test_list))
k = 5
n = len (test_list)
for i in range ( 0 , n - k ):
test_list.pop()
print ("The truncated list is : " + str (test_list))
|
Output:
The original list is : [1, 4, 5, 6, 7, 3, 8, 9, 10]
The truncated list is : [1, 4, 5, 6, 7]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using pop() function which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
Method #2 : Using del + list slice del operator can be used to delete all the elements that appear after the specific index, which is handled by the list slicing technique.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 3 , 8 , 9 , 10 ]
print ("The original list is : " + str (test_list))
k = 5
del test_list[ 5 :]
print ("The truncated list is : " + str (test_list))
|
Output:
The original list is : [1, 4, 5, 6, 7, 3, 8, 9, 10]
The truncated list is : [1, 4, 5, 6, 7]
Method #3 : Using list slicing In this method, we just slice the first K elements and ignore all the elements that occur after K elements. This is most pythonic and recommended method to perform this task.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 3 , 8 , 9 , 10 ]
print ("The original list is : " + str (test_list))
k = 5
res = test_list[ 0 : 5 ]
print ("The truncated list is : " + str (res))
|
Output:
The original list is : [1, 4, 5, 6, 7, 3, 8, 9, 10]
The truncated list is : [1, 4, 5, 6, 7]
Method #4 : Using inbuilt slice() function
Here is an example of using the slice() function to truncate a list in Python:
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 3 , 8 , 9 , 10 ]
print ( "The original list is:" , test_list)
truncated_list = slice ( 5 )
res = test_list[truncated_list]
print ( "The truncated list is:" , res)
|
Output
The original list is: [1, 4, 5, 6, 7, 3, 8, 9, 10]
The truncated list is: [1, 4, 5, 6, 7]
You can also specify the start and end indices of the slice using the start and stop arguments of the slice() function, respectively. For example, to truncate the list to the elements between the 3rd and 8th indices (inclusive), you can use the following code:
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 3 , 8 , 9 , 10 ]
print ( "The original list is:" , test_list)
truncated_list = slice ( 3 , 8 )
res = test_list[truncated_list]
print ( "The truncated list is:" , res)
|
Output
The original list is: [1, 4, 5, 6, 7, 3, 8, 9, 10]
The truncated list is: [6, 7, 3, 8, 9]
Method #6: Using numpy:
Algorithm :
- Initialize the list test_list with some values.
- Print the original list.
- Set a value k to determine the size of the truncated list.
- Use the del keyword and list slicing to remove elements from index k onwards in the list.
- Print the truncated list.
Python3
import numpy as np
test_array = np.array([ 1 , 4 , 5 , 6 , 7 , 3 , 8 , 9 , 10 ])
print ( "The original array is: " , test_array)
k = 5
truncated_array = test_array[:k]
print ( "The truncated array is: " , truncated_array)
|
Output:
The original array is: [ 1 4 5 6 7 3 8 9 10]
The truncated array is: [1 4 5 6 7]
Time complexity:
The initialization of the list takes O(n) time, where n is the number of elements in the list.
The printing of the original list takes O(n) time.
The truncation of the list using del and slicing takes O(n-k) time, where k is the number of elements to be kept in the list.
The printing of the truncated list takes O(k) time.
Therefore, the overall time complexity of the code is O(n).
Space complexity:
The initialization of the list takes O(n) space, where n is the number of elements in the list.
The k variable takes O(1) space.
The truncation of the list is performed in-place, so no additional space is used.
Therefore, the overall space complexity of the code is O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...