{"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>
\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>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = [0, 0, 1, 1, 0, 1, 1, 1, 1, 0]<\/pre>\n

Output:<\/strong><\/p>\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

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = [1 0 1 0 0 0 1 1 0 1]<\/pre>\n

Output:<\/strong><\/p>\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

Below are the ways to separate the given list into 0s on the left and 1s on the right.<\/p>\n