
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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]
- Related Articles
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Sort an arrays of 0’s, 1’s and 2’s using Java
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Construct DFA of alternate 0’s and 1’s
- Find the Pattern of 1’s inside 0’s using C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
- Given a sorted array of 0’s and 1’s, find the transition point of the array in C++
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Maximum length of segments of 0’s and 1’s in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Maximum 0’s between two immediate 1’s in binary representation in C++
- C++ Largest Subtree having Equal No of 1's and 0's
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
