Understanding a Python List | Separate 0 and 1 in a list of arrays



Examples:

 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 a solution for this problem, please refer to split 0 and 1 in link per array . We can quickly fix this problem in Python using

# Function for separating 0 and 1 in a list of arrays

 

def segregate (arr):

res = ([x for x in arr if x = = 0 ] + [x for x in arr if x = = 1 ])

print (res)

 
# Driver program

if __ name__ = = "__ main__" :

arr = [ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 0

  segregate (arr)

Output:

 [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]