# Segregate 0’s and 1’s in an array list using Python?

The elements in the contiguous memory address are contained in the linear data structure known as an array. At these places, it primarily groups components of the same data type.

Given an array of integers. The array is to be divided into two halves, 0s and 1s, according to the article "Segregate 0s and 1s in an array." The array should have all the 0â€™s on the left and all the 1â€™s on the right.

### Input-Output Scenario

Letâ€™s consider an input and its output scenarios to segregate 0â€™s and 1â€™s in an array list -

Input: [0,1,1,0,0,1,0,0,0]
Output: [0,0,0,0,0,0,1,1,1]


As we can see in the output that all the 0â€™s and the 1â€™s have been segregated in the array list with 0â€™s being in the left and 1â€™s in the right.

In this article we will discuss various methods to segregate 0â€™s and 1â€™s in an array list in Python.

## By counting 0â€™s and 1â€™s

Find the total number of 0s. Count will be M. After determining the count, we can place M 0s at the beginning and 1s at the remaining n â€“ M spots in the array.

### Example

Following is an example to segregate 0â€™s and 1â€™s in an array list using count 0â€™s and 1â€™s method -

def segregating(array, x) :
# Counting the 0's in array
count = 0
for i in range(0, x) :
if (array[i] == 0) :
count = count + 1

# Loop for segregationg all the 0's
for i in range(0, count) :
array[i] = 0

# Loop for segregationg all the 1's
for i in range(count, x) :
array[i] = 1

# Function for printing the segregated array
def print_the_array(array , x) :
print( "The segregated array is :",end = "")

for i in range(0, x) :
print(array[i] , end = " ")

# The driver function
array = [0,1,1,0,0,1,0,0,0]
x = len(array)

segregating(array, x)
print_the_array(array, x)


### Output

Following is an output of the above code -

The segregated array is :0 0 0 0 0 0 1 1 1


## Using 2 indexes for traversing

The position of the element in the given list or the characters in the string are returned by Python's index() function.

In order to check or use the data as part of a process, it is necessary to access each element (item) stored in an array. This is known as to traverse through an array.

### Algorithm

Following is an approach to segregate 0â€™s and 1â€™s in an array list by traversing through an array using 2 indexes -

• Keep two indexes. Set the first index on the left to 0 and the second index on the right to n-1.

• Follow when moving left or right.

• While there are 0s available, keep increasing the index left.

• Continue decreasing the index right while there are 1s available.

• Exchange arr[left] and arr[right] if left < right.

### Example

Following is an example to segregate 0â€™s and 1â€™s in an array list by traversing through an array using 2 indexes -

def segregating(array, s):

# Initializing both the left and the right index
left, right = 0, s-1
while left < right:

# Incrementing the left index while seeing 0 at the left
while array[left] == 0 and left < right:
left += 1

# Decrementing right index while seeing 1 at right
while array[right] == 1 and left < right:
right -= 1
if left < right:
array[left] = 0
array[right] = 1
left += 1
right -= 1
return array

# The driver code
array = [0,1,1,0,0,1,0,0,0]
array_size = len(array)
print("The segregated array is :")
print(segregating(array, array_size))


### Output

Following is an output of the above code -

The segregated array is :
[0, 0, 0, 0, 0, 0, 1, 1, 1]


## Using list comprehension

A common Python technique is list comprehension. Here, we apply this method. We construct an array from user input, and each entry should be a random combination of 0s and 1s. Then group the 0s on the left and the 1s on the right. We iterate through the array to separate two distinct lists, one of which contains 0s and the other 1, and then we concatenate the two lists.

### Example

Following is an example to segregate 0â€™s and 1â€™s in an array list using list comprehension -

# Segregate all the 0's and 1's present in an array list
def seg0s1s(A):
n = ([i for i in A if i==0] + [i for i in A if i==1])
print(n)

# Driver program
if __name__ == "__main__":
A=list()
n=int(input("Enter the size of the array ::"))
print("Enter the number ::")
for i in range(int(n)):
k=int(input(""))
A.append(int(k))
print("The New ArrayList ::")
seg0s1s(A)


### Output

Following is an output of the above code -

Enter the size of the array ::7
Enter the number ::
1
0
1
0
0
1
1
The New ArrayList ::
[0, 0, 0, 1, 1, 1, 1]