Friday, July 5, 2024
HomeData ModellingData Structure & AlgorithmMove all zeroes to end of array using List Comprehension in Python

Move all zeroes to end of array using List Comprehension in Python

Given an array of random numbers, Push all the zeros of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1). Examples:

Input :  arr = [1, 2, 0, 4, 3, 0, 5, 0]
Output : arr = [1, 2, 4, 3, 5, 0, 0, 0]

Input : arr  = [1, 2, 0, 0, 0, 3, 6]
Output : arr = [1, 2, 3, 6, 0, 0, 0]

We have existing solution for this problem please refer Move all zeroes to end of array link. We will solve this problem in python using List Comprehension in a single line of code. 

Implementation:

Python




# Function to append all zeros at the end
# of array
def moveZeros(arr):
     
    # first expression returns a list of
    # all non zero elements in arr in the
    # same order they were inserted into arr
    # second expression returns a list of
    # zeros present in arr
    return [nonZero for nonZero in arr if nonZero!=0] + \
        [Zero for Zero in arr if Zero==0]
 
# Driver function
if __name__ == "__main__":
    arr = [1, 2, 0, 4, 3, 0, 5, 0]
    print (moveZeros(arr))


Output

[1, 2, 4, 3, 5, 0, 0, 0]

This article is contributed by Shashank Mishra (Gullu). If you like neveropen and would like to contribute, you can also write an article using write.neveropen.co.za or mail your article to review-team@neveropen.co.za. See your article appearing on the neveropen main page and help other Geeks.

Approach#2: Using while loop

This approach moves all zeroes in an input array to the end of the array while keeping the non-zero elements in their original order. It uses two pointers to keep track of the positions of the zeroes and non-zeroes in the array, and swaps them as necessary.

Algorithm

1. Initialize two pointers i and j to 0.
2. Traverse the array with j and for each non-zero element, swap arr[i] and arr[j] and increment i.
3. Return the resulting array.

Python3




def move_zeroes(arr):
    i, j = 0, 0
    while j < len(arr):
        if arr[j] != 0:
            arr[i], arr[j] = arr[j], arr[i]
            i += 1
        j += 1
    return arr
arr=[1, 2, 0, 4, 3, 0, 5, 0]
print(move_zeroes(arr))


Output

[1, 2, 4, 3, 5, 0, 0, 0]

Time Complexity: O(n)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments