Python | Split indices of true and false value



Method # 1: Using a Loop
This is the only way this task can be accomplished. We create different lists and check for True or False using conditional operators and add its index to the selected lists accordingly.

# Python3 code to demonstrate how it works
# Separate True and False indexes
# using loops

 
# initialize the list

test_list = [ False , True , False , False , True , True ]

  
# print the original list

print ( " The original list is: " + str (test_list) )

 
# Separate indices for True and False values ​​
# 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)

  
# print result

print ( "True indices after grouping:" + str (res_true))

print ( "False indices after grouping:" + str (res_false))

 

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] 

Method # 2: Using loop + enumerate()
This task can be roughly solved using the above functions. In this we make the choice of adding a list and, accordingly, add elements to the selected lists.

# Python3 code to demonstrate how it works
# Separate indices of True and False values ​​
# using loop + enumerate ()

 
# initialize the list

test_list = [ False , True , False , False , True , True ]

 
# print original list

print ( "The original list is:" + str (test_list))

  
# Separate indices of True and False values ​​
# 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)

  
# print result

print ( "True indices after grouping:" + str (res_true))

print ( "False indices after grouping:" + str (res_false))

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] < / pre>