Open In App

Python | Truncate a list

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# to truncate list using pop()
 
# initializing list 
test_list = [1, 4, 5, 6, 7, 3, 8, 9, 10]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# size desired
k = 5
 
# using pop()
# to truncate list
n = len(test_list)
for i in range(0, n - k ):
    test_list.pop()
 
# printing result
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




# Python3 code to demonstrate
# to truncate list
# using del + list slicing
 
# initializing list 
test_list = [1, 4, 5, 6, 7, 3, 8, 9, 10]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# size desired
k = 5
 
# using del + list slicing
# to truncate list
del test_list[5:]
 
# printing result
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




# Python3 code to demonstrate
# to truncate list
# using list slicing
 
# initializing list 
test_list = [1, 4, 5, 6, 7, 3, 8, 9, 10]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# size desired
k = 5
 
# using list slicing
# to truncate list
res = test_list[0 : 5]
 
# printing result
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




# Initialize a list
test_list = [1, 4, 5, 6, 7, 3, 8, 9, 10]
 
# Print the original list
print("The original list is:", test_list)
 
# Truncate the list to the first 5 elements
truncated_list = slice(5)
res = test_list[truncated_list]
 
# Print the truncated list
print("The truncated list is:", res)
#This code is contributed by Edula Vinay Kumar Reddy


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




# Initialize a list
test_list = [1, 4, 5, 6, 7, 3, 8, 9, 10]
 
# Print the original list
print("The original list is:", test_list)
 
# Truncate the list to the elements between the 3rd and 8th indices (inclusive)
truncated_list = slice(3, 8)
res = test_list[truncated_list]
 
# Print the truncated list
print("The truncated list is:", res)
 
#This code is contributed by Edula Vinay Kumar Reddy


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 :

  1. Initialize the list test_list with some values.
  2. Print the original list.
  3. Set a value k to determine the size of the truncated list.
  4. Use the del keyword and list slicing to remove elements from index k onwards in the list.
  5. Print the truncated list.

Python3




import numpy as np
 
# initializing numpy array
test_array = np.array([1, 4, 5, 6, 7, 3, 8, 9, 10])
 
# printing original array
print("The original array is: ", test_array)
 
# size desired
k = 5
 
# using numpy slicing to truncate array
truncated_array = test_array[:k]
 
# printing result
print("The truncated array is: ", truncated_array)
#This code is contributed by Jyothi pinjala.


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
Previous
Next
Share your thoughts in the comments

Similar Reads