Python | Move given element to List Start
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 listtest_list = [1, 4, None, "Manjeet ",False, 4, False, 4,"Nikhil"]# printing original listprint("The original list : " + str(test_list))# initializing KK = 4# using list comprehension + isinstance()# Move all K element to List Starttemp = [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 resultprint("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.
Python3
# Python3 code to demonstrate# Move all K element to List Start# using list comprehension + isinstance() + list slicing# initializing listtest_list = [1, 4, None, "Manjeet", False, 4, False, 4, "Nikhil"]# printing original listprint("The original list : " + str(test_list))# initializing KK = 4# using list comprehension + isinstance() + list slicing# Move all K element to List Startres = ([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 resultprint("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 listtest_list = [1, 4, None, "Manjeet", False, 4, False, 4, "Nikhil"]# printing original listprint("The original list : " + str(test_list))# initializing KK = 4x = test_list.count(K)while K in test_list: test_list.remove(K)res = [K]*xres.extend(test_list)# print resultprint("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 complexity: 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 listtest_list = [1, 4, None, "Manjeet", False, 4, False, 4, "Nikhil"]# printing original listprint("The original list : " + str(test_list))# initializing KK = 4a=[]b=[]for i in test_list: if(i==K): a.append(i) else: b.append(i)a.extend(b)# print resultprint("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']
The time complexity of the above Python3 code is O(n), where n is the length of the input list test_list.
The space complexity of the code is O(n), because it creates two additional lists, a and b, which together can hold all the elements of the input list.



.gif)

Please Login to comment...