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]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Segregate list elements by Suffix
- Python | Segregate True and False value indices
- Python | Segregate Positive and Negative Integers from mixed string
- Python - Segregate elements by delimiter
- Move all zeroes to end of array using List Comprehension in Python
- Python List Comprehension | Three way partitioning of an array around a given range
- Python List Comprehension and Slicing
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Difference between List comprehension and Lambda in Python
- Count set bits using Python List comprehension
- Python List Comprehension to find pair with given sum from two arrays
- Python | List comprehension vs * operator
- Python - Map vs List comprehension
- How to handle a Python Exception in a List Comprehension?
- Python - List Comprehension
- Python Dictionary Comprehension
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
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.