{"id":18224,"date":"2021-08-26T10:42:10","date_gmt":"2021-08-26T05:12:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=18224"},"modified":"2021-11-22T18:37:14","modified_gmt":"2021-11-22T13:07:14","slug":"python-program-to-sort-a-list-containing-two-types-of-elements","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-a-list-containing-two-types-of-elements\/","title":{"rendered":"Python Program to Sort a List Containing Two Types of Elements"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Replace Every List Element by Multiplication of Previous and Next<\/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 Below are the ways to separate the given list into 0s on the left and 1s on the right.<\/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 Replace Every List Element by Multiplication of Previous and Next Given a random list of 0s and 1s and the task is to separate the list into 0s on the left and 1s on the right. Only traverse the list once. Examples: Example1: Input: Given …<\/p>\n
\nGiven a random list of 0s and 1s and the task is to separate the list into 0s on the left and 1s on the right. Only traverse the list once.<\/p>\nGiven List = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]<\/pre>\n
The above given list after separation of 0s on the left and 1s on the right:\r\n0 0 0 0 1 1 1 1 1 1<\/pre>\n
Given List = [1 0 1 0 0 0 1 1 0 1]<\/pre>\n
The above given list after separation of 0s on the left and 1s on the right:\r\n0 0 0 0 0 1 1 1 1 1<\/pre>\n
Program to Sort a List Containing Two Types of Elements in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngivn_lst = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]\r\n# Calculate the length of the given list using the len() function and store it in\r\n# another variable.\r\nlent_lst = len(givn_lst)\r\n# Take a variable say 'p' and initialize its value with 0.\r\np = 0\r\n# Take another variable 'q' and initialize its value with the length of the given list -1.\r\n# Store it in another variable.\r\nq = lent_lst - 1\r\n# Check if the value of p is less than q using the while loop.\r\nwhile (p < q):\r\n # If the statement is true, check again if the value of the given list of p is equal\r\n # to 1 using the if conditional statement.\r\n if(givn_lst[p] == 1):\r\n # If the statement is true, then assign a given list of p with the given list of q and\r\n # a given list of q with the given list of p.\r\n givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]\r\n # Decrement the value of q by 1 and store it in the same variable 'q\r\n q -= 1\r\n # Else increment the value of p by 1 and store it in the same variable 'p'.\r\n # Exit the while Loop.\r\n else:\r\n p += 1\r\nprint(\"The above given list after separation of 0s on the left and 1s on the right:\")\r\n# Loop from 0 to the length of the given list using the for loop.\r\nfor itror in range(0, lent_lst):\r\n # Inside the loop, print the given list of iterator values.\r\n print(givn_lst[itror], end=\" \")\r\n<\/pre>\n
The above given list after separation of 0s on the left and 1s on the right:\r\n0 0 0 0 1 1 1 1 1 1<\/pre>\n
Method #2: Using While loop (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\ngivn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given list using the len() function and store it in\r\n# another variable.\r\nlent_lst = len(givn_lst)\r\n# Take a variable say 'p' and initialize its value with 0.\r\np = 0\r\n# Take another variable 'q' and initialize its value with the length of the given list -1.\r\n# Store it in another variable.\r\nq = lent_lst - 1\r\n# Check if the value of p is less than q using the while loop.\r\nwhile (p < q):\r\n # If the statement is true, check again if the value of the given list of p is equal\r\n # to 1 using the if conditional statement.\r\n if(givn_lst[p] == 1):\r\n # If the statement is true, then assign a given list of p with the given list of q and\r\n # a given list of q with the given list of p.\r\n givn_lst[p], givn_lst[q] = givn_lst[q], givn_lst[p]\r\n # Decrement the value of q by 1 and store it in the same variable 'q\r\n q -= 1\r\n # Else increment the value of p by 1 and store it in the same variable 'p'.\r\n # Exit the while Loop.\r\n else:\r\n p += 1\r\nprint(\"The above given list after separation of 0s on the left and 1s on the right:\")\r\n# Loop from 0 to the length of the given list using the for loop.\r\nfor itror in range(0, lent_lst):\r\n # Inside the loop, print the given list of iterator values.\r\n print(givn_lst[itror], end=\" \")\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 0 1 0 0 0 1 1 0 1\r\nThe above given list after separation of 0s on the left and 1s on the right:\r\n0 0 0 0 0 1 1 1 1 1<\/pre>\n
\n