Python | Segregate True and False value indices
Sometimes, while working with Python lists, we can have a problem in which we have boolean list and require to have indices of True and False values separately. This can have a possible use in Data Science domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop This is the one way in which this task can be performed. We create different lists and check for True or False values using conditional statements and append it’s index accordingly in dedicated lists.
Python3
# Python3 code to demonstrate working of # Segregate True and False value indices # using loops # initialize list test_list = [ False , True , False , False , True , True ] # printing original list print ("The original list is : " + str (test_list)) # Segregate True and False value indices # using loops res_true, res_false = [], [] for i in range ( 0 , len (test_list)): if test_list[i]: res_true.append(i) else : res_false.append(i) # printing result print (" True indices after grouping : " + str (res_true)) print (" False indices after grouping : " + str (res_false)) |
The original list is : [False, True, False, False, True, True] True indices after grouping : [1, 4, 5] False indices after grouping : [0, 2, 3]
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list
Method #2 : Using loop + enumerate() This task can be solved in a brute manner by using above functionalities. In this, we make a choice of append list and add elements accordingly in dedicated lists.
Python3
# Python3 code to demonstrate working of # Segregate True and False value indices # using loop + enumerate() # initialize list test_list = [ False , True , False , False , True , True ] # printing original list print ("The original list is : " + str (test_list)) # Segregate True and False value indices # using loop + enumerate() res_true, res_false = [], [] for i, ele in enumerate (test_list): temp = res_true if ele else res_false temp.append(i) # printing result print (" True indices after grouping : " + str (res_true)) print (" False indices after grouping : " + str (res_false)) |
The original list is : [False, True, False, False, True, True] True indices after grouping : [1, 4, 5] False indices after grouping : [0, 2, 3]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. loop + enumerate() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #4: Using numpy.where()
Note: Install numpy module using command “pip install numpy”
This method uses numpy library’s where function. This function returns the indices of elements in the input array that satisfy the given condition.
Python3
import numpy as np # initialize list test_list = [ False , True , False , False , True , True ] # printing original list print ( "The original list is : " + str (test_list)) # Segregate True and False value indices # using numpy.where() res_true = np.where(test_list)[ 0 ] res_false = np.where(np.invert(test_list))[ 0 ] # printing result print ( "True indices after grouping : " + str (res_true)) print ( "False indices after grouping : " + str (res_false)) #This code is contributed by Edula Vinay Kumar Reddy |
Output : The original list is : [False, True, False, False, True, True] True indices after grouping : [1 4 5] False indices after grouping : [0 2 3]
This method is more elegant and efficient way than previous methods and it’s time complexity is O(n) and space complexity is O(n)
Method#4: Using Recursive method.
Algorithm:
- Define a function named “segregate_indices” that takes a list, an index variable i (initialized to 0), and two empty lists named res_true and res_false as input parameters.
- Check if the value of index i is equal to the length of the list. If yes, return the two lists res_true and res_false.
- Else, check if the value of the element at index i of the list is True. If yes, append the value of i to the res_true list. Else, append the value of i to the res_false list.
- Recursively call the function with updated values of i, res_true, and res_false.
- After the recursive calls complete, return the two lists res_true and res_false.
Python3
def segregate_indices(test_list, i = 0 , res_true = [], res_false = []): if i = = len (test_list): return res_true, res_false elif test_list[i]: res_true.append(i) else : res_false.append(i) return segregate_indices(test_list, i + 1 , res_true, res_false) # initialize list test_list = [ False , True , False , False , True , True ] # printing original list print ( "The original list is : " + str (test_list)) # Segregate True and False value indices # using recursion res_true, res_false = segregate_indices(test_list) # printing result print ( "True indices after grouping : " + str (res_true)) print ( "False indices after grouping : " + str (res_false)) |
The original list is : [False, True, False, False, True, True] True indices after grouping : [1, 4, 5] False indices after grouping : [0, 2, 3]
The time complexity of the algorithm is O(n), where n is the length of the input list. This is because the function traverses the entire input list once.
The auxiliary space of the algorithm is also O(n), because in the worst case scenario, all elements of the input list could be either True or False, and both res_true and res_false lists would have to store all n indices.
Method#5: Using heapq:
Algorithm:
- Import heapq module.
- Initialize an empty heap for both true and false indices.
- Loop through the original list, and for each index:
a. If the value is True, push the index to the heap of true indices.
b. If the value is False, push the index to the heap of false indices. - Print the true and false indices.
Python3
import heapq # initialize list test_list = [ False , True , False , False , True , True ] # printing original list print ( "The original list is : " + str (test_list)) # segregate True and False value indices using heapq method res_true, res_false = [], [] for i, val in enumerate (test_list): heapq.heappush(res_true, i) if val else heapq.heappush(res_false, i) # printing result print ( "True indices after grouping : " + str ( list (res_true))) print ( "False indices after grouping : " + str ( list (res_false))) #This code is contributed by Jyothi pinjala. |
The original list is : [False, True, False, False, True, True] True indices after grouping : [1, 4, 5] False indices after grouping : [0, 2, 3]
Time Complexity: The time complexity of the algorithm is O(n log n), where n is the length of the input list. The loop takes O(n) time, and the heap push operation takes O(log n) time.
Space Complexity: The space complexity of the algorithm is O(n), where n is the length of the input list. This is because we are storing the true and false indices in separate heaps.
Contact Us