Python | Shift zeroes at end of list
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 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)) |
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 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)) |
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]
Recommended Posts:
- Python | Shift sublist in list
- Python | Shift last element to first position in list
- Move all zeroes to end of array using List Comprehension in Python
- Python | Pandas Index.shift()
- Python | Pandas TimedeltaIndex.shift()
- Python | Pandas dataframe.shift()
- Python | Decimal shift() method
- Python | Pandas Series.shift()
- Python Program to Count trailing zeroes in factorial of a number
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of numerical string to list of Integers
- Python | Pair and combine nested list to tuple list
- Python | Convert list of string into sorted list of integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



