{"id":18187,"date":"2021-08-26T10:42:21","date_gmt":"2021-08-26T05:12:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=18187"},"modified":"2021-11-22T18:37:13","modified_gmt":"2021-11-22T13:07:13","slug":"python-program-to-move-all-negative-elements-to-end-in-order-with-extra-space-allowed","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-move-all-negative-elements-to-end-in-order-with-extra-space-allowed\/","title":{"rendered":"Python Program to Move all Negative Elements to End in Order with Extra Space Allowed"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Split the Array\/List and add the First Part to the End<\/a> 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 the negative elements to the end of the list some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Split the Array\/List and add the First Part to the End Given a list that contains the negative and positive elements the task is to move all the negative elements to the end of the list. Examples: Example1: Input: Given List =[1, 9, -3, 6, …<\/p>\n
\nGiven a list that contains the negative and positive elements the task is to move all the negative elements to the end of the list.<\/p>\nGiven List =[1, 9, -3, 6, 8, 11, 35, -5, -7, 10, -1, -2, -3]<\/pre>\n
The given list after moving negative elements to the end is [1, 9, 6, 8, 11, 35, 10, -3, -5, -7, -1, -2, -3]<\/pre>\n
Given List = [-7 11 56 -3 -8 12 17 -25 -8 -11 -6 9 10]<\/pre>\n
The given list after moving negative elements to the end is [11, 56, 12, 17, 9, 10, -7, -3, -8, -25, -8, -11, -6]<\/pre>\n
Program to Move all Negative Elements to End in Order with Extra Space Allowed in Python<\/h2>\n
\n
Method #1: Using List Comprehension (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvnlst = [1, 9, -3, 6, 8, 11, 35, -5, -7, 10, -1, -2, -3]\r\n# Using list comprehension and if conditional statements\r\n# separate the positive numbers from the list\r\n# and store it in a variable to say the positive list.\r\npstivelist = [elemn for elemn in gvnlst if elemn >= 0]\r\n# Using list comprehension and if conditional statements\r\n# separate the negative numbers from the list and store it\r\n# in a variable to say the negative list.\r\nngtivelist = [elemn for elemn in gvnlst if elemn < 0]\r\n# Add the positive list and negative list using the + operator\r\n# and store it in another variable to say result list\r\n# (This operation moves all the negative list).\r\nresltlist = pstivelist+ngtivelist\r\n# Print the result list.\r\nprint('The given list after moving negative elements to the end is', resltlist)\r\n<\/pre>\n
The given list after moving negative elements to the end is [1, 9, 6, 8, 11, 35, 10, -3, -5, -7, -1, -2, -3]<\/pre>\n
Method #2: Using List Comprehension (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlstt = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Using list comprehension and if conditional statements\r\n# separate the positive numbers from the list\r\n# and store it in a variable to say the positive list.\r\npstivelist = [elemn for elemn in gvnlstt if elemn >= 0]\r\n# Using list comprehension and if conditional statements\r\n# separate the negative numbers from the list and store it\r\n# in a variable to say the negative list.\r\nngtivelist = [elemn for elemn in gvnlstt if elemn < 0]\r\n# Add the positive list and negative list using the + operator\r\n# and store it in another variable to say result list\r\n# (This operation moves all the negative list).\r\nresltlist = pstivelist+ngtivelist\r\n# Print the result list.\r\nprint('The given list after moving negative elements to the end is', resltlist)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = -7 11 56 -3 -8 12 17 -25 -8 -11 -6 9 10\r\nThe given list after moving negative elements to the end is [11, 56, 12, 17, 9, 10, -7, -3, -8, -25, -8, -11, -6]<\/pre>\n
\n