{"id":8309,"date":"2021-09-30T16:00:00","date_gmt":"2021-09-30T10:30:00","guid":{"rendered":"https:\/\/python-programs.com\/?p=8309"},"modified":"2021-11-22T18:34:26","modified_gmt":"2021-11-22T13:04:26","slug":"python-program-to-move-all-zeros-present-in-an-array-list-to-the-end","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-move-all-zeros-present-in-an-array-list-to-the-end\/","title":{"rendered":"Python Program to Move all Zeros present in an Array\/List to the End"},"content":{"rendered":"
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples<\/a> as it includes basic to advanced levels of concepts.<\/p>\n List in Python:<\/strong><\/p>\n Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have the immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n Given a list the task is to move all zeros present in the given list to the end in python<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to move all zeros present in the given array\/list to the end some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n The concept is straightforward, if the current element is non-zero, move it to the next available location in the array. After all of the array’s elements have been processed, increment all remaining indices by 0.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n By changing Quicksort’s partitioning algorithm, we can solve this problem in a single scan of the array. The plan is to use 0 as a pivot element and run the partition procedure once. The partitioning logic scans all elements and swaps every non-pivot element with the pivot’s first occurrence.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts. List in Python: Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have the immutability constraints. Lists are often used to …<\/p>\ngiven list = [7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]<\/pre>\n
the list before modification :\r\n[7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]\r\nthe list after modification :\r\n[7, 2, 34, 56, 12, 5, 6, 8, 9, 1, 2, 4, 5, 0, 0, 0, 0, 0]<\/pre>\n
given list = [0, 54, 0, 6, 0, 7, 0, 0, 0, 9, 9, 9, 8, 8, 45, 33, 77, 66, 88, 11, 21, 0, 0, 0, 0]<\/pre>\n
the list before modification :\r\n[0, 54, 0, 6, 0, 7, 0, 0, 0, 9, 9, 9, 8, 8, 45, 33, 77, 66, 88, 11, 21, 0, 0, 0, 0]\r\nthe list after modification :\r\n[54, 6, 7, 9, 9, 9, 8, 8, 45, 33, 77, 66, 88, 11, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]<\/pre>\n
Python Program to Move all Zeros present in an Array\/List to the End<\/h2>\n
\n
Method #1: Taking New List and using conditional statements<\/h3>\n
\n
# Create a new list and call it emptylist\r\nemptylist = []\r\n# given list\r\ngiven_list = [7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]\r\n# print the list before modifictaion\r\nprint(\"the list before modification :\")\r\nprint(given_list)\r\n# Take a count, say zeroCount, and set it to 0 (which counts the\r\n# total amount of zeros in the given list).\r\nzeroCount = 0\r\n# Traverse the given array\/list using for loop.\r\nfor eleme in given_list:\r\n # Using the if condition, determine whether the element is zero or not.\r\n if(eleme != 0):\r\n # If the element is not equal to zero, use the\r\n # append() function to append it to the emptylist.\r\n emptylist.append(eleme)\r\n # Otherwise, increase the zeroCount by one.\r\n else:\r\n zeroCount = zeroCount+1\r\n# adding the zeros to the end of the empty list zeroCount number of times\r\nfor i in range(zeroCount):\r\n emptylist.append(0)\r\n# print the list after modifictaion\r\nprint(\"the list after modification :\")\r\nprint(emptylist)\r\n<\/pre>\n
the list before modification :\r\n[7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]\r\nthe list after modification :\r\n[7, 2, 34, 56, 12, 5, 6, 8, 9, 1, 2, 4, 5, 0, 0, 0, 0, 0]<\/pre>\n
Method #2: Using Quicksort’s partitioning logic<\/h3>\n
\n
\nTraverse the given array\/list using for loop.<\/li>\n# given list\r\ngiven_list = [7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]\r\n# print the list before modifictaion\r\nprint(\"the list before modification :\")\r\nprint(given_list)\r\n# Take a count, say zeroCount, and set it to 0\r\nindex = 0\r\n# Traverse the given array\/list using for loop.\r\nfor eleme in range(len(given_list)):\r\n # Using the if condition, determine whether the element is zero or not.\r\n if(given_list[eleme] != 0):\r\n # swappping index element with current index\r\n given_list[index], given_list[eleme] = given_list[eleme], given_list[index]\r\n # increment the index value by 1\r\n index = index+1\r\n# print the list after modifictaion\r\nprint(\"the list after modification :\")\r\nprint(given_list)\r\n<\/pre>\n
the list before modification :\r\n[7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]\r\nthe list after modification :\r\n[7, 2, 34, 56, 12, 5, 6, 8, 9, 1, 2, 4, 5, 0, 0, 0, 0, 0]<\/pre>\n
\n