# Python | Split indices of true and false value

| | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

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] `

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python | Split indices of true and false value, check other Loops-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Chen Schteiner

Warsaw | 2022-12-08

Thanks for explaining! I was stuck with Python | Split indices of true and false value for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Olivia Lehnman

New York | 2022-12-08

I was preparing for my coding interview, thanks for clarifying this - Python | Split indices of true and false value in Python is not the simplest one. Will get back tomorrow with feedback

Javier OConnell

New York | 2022-12-08

Thanks for explaining! I was stuck with Python | Split indices of true and false value for some hours, finally got it done 🤗. Will use it in my bachelor thesis

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

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

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries