You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array.
Input : arr = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output : [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
We have existing solution for this problem please refer Segregate 0s and 1s in an array link. We can solve this problem quickly in Python using List Comprehension. Traverse given list and separate out two different lists, one contains all 0’s and another one contains all 1’s. Now concatenate both lists together.
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
- Python List Comprehension | Three way partitioning of an array around a given range
- Move all zeroes to end of array using List Comprehension in Python
- Python | Segregate list elements by Suffix
- Python List Comprehension and Slicing
- Count set bits using Python List comprehension
- Python List Comprehension to find pair with given sum from two arrays
- List comprehension and ord() in Python to remove all characters other than alphabets
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Add list elements with a multi-list based on index
- Python | Pair and combine nested list to tuple list
- Python | Sorting list of lists with similar list elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.