Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Shift zeroes at end of list

Improve Article
Save Article
  • Last Updated : 07 Jan, 2023
Improve Article
Save Article

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 0’s at end of list, catch here is it checks for just 0’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 front and at end we just push the zeroes to end. The isinstance method is used to filter out the Boolean False entity. 

Python3




# Python3 code to demonstrate
# Shift zeroes at end of list
# using list comprehension + isinstance()
 
# initializing list
test_list = [1, 4, None, "Manjeet", False, 0, False, 0, "Nikhil"]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + isinstance()
# Shift zeroes at end of list
temp = [ele for ele in test_list if ele or
        ele is None or isinstance(ele, bool)]
res = temp + [0] * (len(test_list) - len(temp))
 
# print result
print("The list after shifting 0's to end : " + str(res))


Output : 

The original list : [1, 4, None, ‘Manjeet’, False, 0, False, 0, ‘Nikhil’] The list after shifting 0’s to end : [1, 4, None, ‘Manjeet’, False, False, ‘Nikhil’, 0, 0]

  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 0’s to perform whole task in just 1 step. 

Python3




# Python3 code to demonstrate
# Shift zeroes at end of list
# using list comprehension + isinstance() + list slicing
 
# initializing list
test_list = [1, 4, None, "Manjeet", False, 0, False, 0, "Nikhil"]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + isinstance() + list slicing
# Shift zeroes at end of list
res = ([ele for ele in test_list if not isinstance(ele, int)
        or ele or isinstance(ele, bool)]
        + [0] * len(test_list))[:len(test_list)]
 
# print result
print("The list after shifting 0's to end : " + str(res))


Output : 

The original list : [1, 4, None, ‘Manjeet’, False, 0, False, 0, ‘Nikhil’] The list after shifting 0’s to end : [1, 4, None, ‘Manjeet’, False, False, ‘Nikhil’, 0, 0]

Method #3 : Using counting 0’s and appending at end

  1. Initialize an empty list called result and a counter count to keep track of the number of zeroes encountered in the list.
  2. Iterate over the elements in the list. If an element is not zero, append it to the result list. If it is zero, increment the count by 1.
  3. Append [0] * count to the end of the result list. This will add count number of zeroes to the end of the list.

The resulting list will have all the non-zero elements from the original list followed by the zeroes that were originally scattered throughout the list.

Python3




# initializing list
test_list = [1, 4, None, "Manjeet", False, 0, False, 0, "Nikhil"]
 
# printing original list
print("The original list : " + str(test_list))
 
# Shift zeroes at end of list
result = []
count = 0
for ele in test_list:
    if ele == 0:
        count += 1
    else:
        result.append(ele)
result += [0] * count
 
# print result
print("The list after shifting 0's to end : " + str(result))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [1, 4, None, 'Manjeet', False, 0, False, 0, 'Nikhil']
The list after shifting 0's to end : [1, 4, None, 'Manjeet', 'Nikhil', 0, 0, 0, 0]

This approach has a time complexity of O(n), where n is the number of elements in the list, because it requires one iteration over the elements in the list. The space complexity is also O(n), because a new list with size equal to the number of elements in the original list is created.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!