Python List Comprehension | Segregate 0’s and 1’s in an array list

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.





# Function to Segregate 0's and 1's in an array list
def segregate(arr):
    res = ([x for x in arr if x==0] + [x for x in arr if x==1])
# Driver program
if __name__ == "__main__":
    arr = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0



[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.

My Personal Notes arrow_drop_up

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.