The conventional problem involving the element shifts has been discussed many times earlier, but sometimes we have strict constraints performing them, and knowledge of any possible variation helps. This article talks about one such problem of shifting K’s at the start of the 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 the front. The instance method is used to filter out the Boolean False entity.
Python3
# Python3 code to demonstrate # Move all K element to List Start # using list comprehension + isinstance() # initializing list test_list = [ 1 , 4 , None , "Manjeet " , False , 4 , False , 4 , "Nikhil" ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 4 # using list comprehension + isinstance() # Move all K element to List Start temp = [ele for ele in test_list if ele ! = K and ele or ele is None or isinstance (ele, bool )] res = [K] * ( len (test_list) - len (temp)) + temp # print result print ( "The list after shifting K's to start : " + str (res)) |
The original list : [1, 4, None, 'Manjeet ', False, 4, False, 4, 'Nikhil'] The list after shifting K's to start : [4, 4, 4, 1, None, 'Manjeet ', False, False, 'Nikhil']
Time Complexity: O(n)
Auxiliary Space: O(n)
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.
step-by-step approach
- Initialize the input list test_list as given in the problem statement.
- Print the original list for verification.
- Initialize the value of K to be moved to the start of the list.
- Create a list comprehension that does the following:
- Generate a list of K values with the same length as test_list.
- Append the remaining values of the list that are not equal to K and satisfy one of the following conditions:
- Not an integer or boolean value.
- A boolean value that is False.
- Use list slicing to remove the K values that were added in step 1.
- Assign the resulting list to the variable res.
- Print the resulting list for verification.
Python3
# Python3 code to demonstrate # Move all K element to List Start # using list comprehension + isinstance() + list slicing # initializing list test_list = [ 1 , 4 , None , "Manjeet" , False , 4 , False , 4 , "Nikhil" ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 4 # using list comprehension + isinstance() + list slicing # Move all K element to List Start res = ([K] * len (test_list) + [ele for ele in test_list if ele ! = K and ele or not isinstance (ele, int ) or isinstance (ele, bool )])[ len ([ele for ele in test_list if ele ! = K and ele or not isinstance (ele, int ) or isinstance (ele, bool )]):] # print result print ( "The list after shifting K's to end : " + str (res)) |
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil'] The list after shifting K's to end : [4, 4, 4, 1, None, 'Manjeet', False, False, 'Nikhil']
Time complexity: O(n)
Auxiliary space: O(n)
Method #3 : Using count() , remove(), extend() methods
Python3
# Python3 code to demonstrate # Move all K element to List Start # initializing list test_list = [ 1 , 4 , None , "Manjeet" , False , 4 , False , 4 , "Nikhil" ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 4 x = test_list.count(K) while K in test_list: test_list.remove(K) res = [K] * x res.extend(test_list) # print result print ( "The list after shifting K's to start : " + str (res)) |
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil'] The list after shifting K's to start : [4, 4, 4, 1, None, 'Manjeet', False, False, 'Nikhil']
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(x), where x is the number of occurrences of K in the list.
Method #4 : Using append() and extend() methods
Python3
# Python3 code to demonstrate # Move all K element to List Start # initializing list test_list = [ 1 , 4 , None , "Manjeet" , False , 4 , False , 4 , "Nikhil" ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 4 a = [] b = [] for i in test_list: if (i = = K): a.append(i) else : b.append(i) a.extend(b) # print result print ( "The list after shifting K's to start : " + str (a)) |
The original list : [1, 4, None, 'Manjeet', False, 4, False, 4, 'Nikhil'] The list after shifting K's to start : [4, 4, 4, 1, None, 'Manjeet', False, False, 'Nikhil']
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), because it creates two additional lists, a and b, which together can hold all the elements of the input list.
Method 5: Using filter() and lambda function
Take a list of mixed data types and moves all occurrences of a given element (K) to the beginning of the list, while preserving the order of the other elements. It does this using the filter() and lambda functions to separate the K elements from the non-K elements, and then concatenating the two lists.
Python3
# initializing list test_list = [ 1 , 4 , None , "Manjeet " , False , 4 , False , 4 , "Nikhil" ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 4 # using filter() and lambda function # Move all K element to List Start K_list = list ( filter ( lambda x: x = = K, test_list)) non_K_list = list ( filter ( lambda x: x ! = K, test_list)) res = K_list + non_K_list # print result print ( "The list after shifting K's to start : " + str (res)) |
The original list : [1, 4, None, 'Manjeet ', False, 4, False, 4, 'Nikhil'] The list after shifting K's to start : [4, 4, 4, 1, None, 'Manjeet ', False, False, 'Nikhil']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we are creating two new lists.