The conventional problem involving the element shifts has been discussed many times earlier, but sometimes we have strict constraints to performing them and knowledge of any possible variation helps. This article talks about one such problem of shifting K’s at end of list, catch here is it checks for just K’s excluding the conventional ‘None’ (False) values. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension + isinstance() In this method, we perform the operation of shifting in 2 steps. In 1st step we get all the values that we need to get at the front and at the end we just push the K’s to end. The isinstance method is used to filter out the Boolean False entity.
Python3
test_list = [ 1 , 4 , None , "Manjeet", False , 4 , False , 4 , "Nikhil"]
print ("The original list : " + str (test_list))
K = 4
temp = [ele for ele in test_list if ele ! = K and ele or ele is None or isinstance (ele, bool ) ]
res = temp + [K] * ( len (test_list) - len (temp))
print ("The list after shifting K's to end : " + str (res))
|
Output :
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil']
The list after shifting K's to end : [1, None, 'Manjeet', False, False, 'Nikhil', 4, 4, 4]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the list comprehension + isinstance() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using list comprehension + isinstance() + list slicing This method is similar to the above method, the only modification is that to reduce number of steps, list slicing is used to attach the K’s to perform whole task in just 1 step.
Python3
test_list = [ 1 , 4 , None , "Manjeet", False , 4 , False , 4 , "Nikhil"]
print ("The original list : " + str (test_list))
K = 4
res = ([ele for ele in test_list if ele ! = K and ele or not isinstance (ele, int )
or isinstance (ele, bool )]
+ [K] * len (test_list))[: len (test_list)]
print ("The list after shifting K's to end : " + str (res))
|
Output :
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil']
The list after shifting K's to end : [1, None, 'Manjeet', False, False, 'Nikhil', 4, 4, 4]
Method#3:using list filtering and list concatenation.
Python3
test_list = [ 1 , 4 , None , "Manjeet" , False , 4 , False , 4 , "Nikhil" ]
print ( "The original list : " + str (test_list))
K = 4
res = list ( filter ( lambda x: x ! = K, test_list)) + [K] * test_list.count(K)
print ( "The list after shifting K's to end : " + str (res))
|
Output
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil']
The list after shifting K's to end : [1, None, 'Manjeet', False, False, 'Nikhil', 4, 4, 4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 using NumPy module:
Python3
import numpy as np
test_list = [ 1 , 4 , None , "Manjeet" , False , 4 , False , 4 , "Nikhil" ]
print ( "The original list : " + str (test_list))
K = 4
temp = np.array(test_list)
mask = np.logical_or(temp = = K, np.logical_or(temp is None , np.isin(temp, [ False ])))
temp = temp[~mask].tolist()
res = temp + [K] * ( len (test_list) - len (temp))
print ( "The list after shifting K's to end : " + str (res))
|
Output:
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil']
The list after shifting K's to end : [1, None, 'Manjeet', 'Nikhil', 4, 4, 4, 4, 4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using a loop
Python3
test_list = [ 1 , 4 , None , "Manjeet" , False , 4 , False , 4 , "Nikhil" ]
print ( "The original list : " + str (test_list))
K = 4
k_list = []
non_k_list = []
for elem in test_list:
if elem = = K:
k_list.append(elem)
else :
non_k_list.append(elem)
res = non_k_list + k_list
while len (res) < len (test_list):
res.append(K)
print ( "The list after shifting K's to end : " + str (res))
|
Output
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil']
The list after shifting K's to end : [1, None, 'Manjeet', False, False, 'Nikhil', 4, 4, 4]
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Apr, 2023
Like Article
Save Article