Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Slicing list from Kth element to last element

Python | Slicing list from Kth element to last element

Python list slicing slices the list from start index till end – 1, specified as list elements. So its tricky when we require to also slice the last element of list. Trying to slice till list size + 1 gives an error. Let’s discuss ways in which last element can be included during a list slice. 

Method #1 : Using None During list slicing, giving the desired first index K and specifying ‘None’ as the second argument in slicing works internally as slicing all the elements from K in list till end including it. 

Python3




# Python3 code to demonstrate
# list slicing from K to end
# using None
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# index to begin slicing
K = 2
 
# using None
# to perform list slicing from K to end
res = test_list[K : None]
 
# printing result
print ("The sliced list is : " +  str(res))


Output:

The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]

Time complexity: O(n), where n is the length of test_list.

Auxiliary space: O(n – K), as new list is created to store the sliced sublist.

Method #2 : Leaving the last element empty Usually, not specifying any element as end element of slicing instructs python to include whole list after K in list. But the main drawback in using this is code readability. Hence above method is preferred more than this. 
 

Python3




# Python3 code to demonstrate
# list slicing from K to end
# without specifying last element
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# index to begin slicing
K = 2
 
# without specifying last element 
# to perform list slicing from K to end
res = test_list[K :]
 
# printing result
print ("The sliced list is : " +  str(res))


Output:

The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]

Time complexity: O(n), where n is the length of the test_list. 

Auxiliary space: O(n – K), as we are creating a new list to store the sliced sublist.

Method #3 : Using itertools

Here is an example of using the islice() function from the itertools module to slice a list from the Kth element to the last element:

Python3




from itertools import islice
 
# Initialize the list
test_list = [5, 6, 2, 3, 9]
 
# Index to begin slicing
K = 2
 
# Use islice() to slice the list from the Kth element to the last element
res = list(islice(test_list, K, None))
 
print(res)  # [2, 3, 9]
#This code is contributed by Edula Vinay Kumar Reddy


Output

[2, 3, 9]

Time complexity: O(n)
Auxiliary space: O(n)

Method 4:  Using a for loop to iterate over the list and append elements to a new list starting from the K index.

Step-by-step approach:

  • Initialize an empty list to store the sliced list.
  • Initialize a variable ‘i’ to 0.
  • Iterate over the original list from index ‘K’ to the end using a for loop.
    • Append each element to the empty list initialized in step 1.
    • Increment ‘i’ by 1 after each iteration.
  • Print the sliced list.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# list slicing from K to end
# without specifying last element
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# index to begin slicing
K = 2
 
# using for loop to slice list from K to end
res = []
for i in range(K, len(test_list)):
    res.append(test_list[i])
 
# printing result
print ("The sliced list is : " +  str(res))


Output

The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]

Time complexity: O(n), where ‘n’ is the length of the original list. 
Auxiliary space: O(n), as we are creating a new list to store the sliced elements.

Method #5: Using list comprehension

  1. Initialize the input list and the index K
  2. Use list comprehension to slice the input list from index K to the end.
  3. Assign the sliced list to a variable ‘res’.
  4. Print the sliced list.

Python3




# Python3 code to demonstrate
# list slicing from K to end
# without specifying last element
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# index to begin slicing
K = 2
 
# using list comprehension to slice list from K to end
res = [test_list[i] for i in range(K, len(test_list))]
 
# printing result
print ("The sliced list is : " +  str(res))


Output

The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we are creating a new list to store the sliced element

Method #6: Using numpy:

Algorithm:

  1. Initialize the list.
  2. Initialize the starting index to slice from.
  3. Create a numpy array from the list slicing from the starting index to the end.
  4. Convert the numpy array back to a list.
  5. Print the sliced list.

Python3




import numpy as np
 
# initializing list
test_list = [5, 6, 2, 3, 9]
 
# printing original list
print("The original list is:", test_list)
 
# index to begin slicing
K = 2
 
# using numpy to slice list from K to end
res = np.array(test_list[K:])
 
# printing result
print("The sliced list is:", res.tolist())


Output:

The original list is: [5, 6, 2, 3, 9]
The sliced list is: [2, 3, 9]

Time Complexity: The time complexity of the code is O(n), where n is the number of elements in the list. This is because we are creating a numpy array from the list which takes O(n) time, and then converting the numpy array back to a list which also takes O(n) time. All other operations are constant time.

Space Complexity: The space complexity of the code is O(n), where n is the number of elements in the list. This is because we are creating a numpy array from the list which takes O(n) space, and then converting the numpy array back to a list which also takes O(n) space. All other operations are constant space.

RELATED ARTICLES

Most Popular

Recent Comments