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] 

Python | Split indices of true and false value Loops: Questions

Python | Split indices of true and false value Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method