
- 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
Boolean list initialization in Python
There are scenarios when we need to get a list containing only the Boolean values like true and false. In this article how to create list containing only Boolean values.
With range
We use range function giving it which is the number of values we want. Using a for loop we assign true or false today list as required.
Example
res = [True for i in range(6)] # Result print("The list with binary elements is : \n" ,res)
Output
Running the above code gives us the following result −
The list with binary elements is : [True, True, True, True, True, True]
With * operator
The * operator can repeat the same value required number of times. We use it to create a list with a Boolean value.
Example
res = [False] * 6 # Result print("The list with binary elements is : \n" ,res)
Output
Running the above code gives us the following result −
The list with binary elements is : [False, False, False, False, False, False]
With bytearray
We can also use the byte array function which will give us 0 as default value.
Example
res = list(bytearray(5)) # Result print("The list with binary elements is : \n" ,res)
Output
Running the above code gives us the following result −
The list with binary elements is : [0, 0, 0, 0, 0]
- Related Articles
- Python - List Initialization with alternate 0s and 1s
- Boolean Indexing in Python
- Boolean Values in Python
- Python Boolean Operations
- What are boolean operators in Python?
- Parsing A Boolean Expression in Python
- Python - Contiguous Boolean Range
- Variable initialization in C++
- Collection Initialization in C#
- Zero Initialization in C++
- Uniform Initialization in C++
- Is there a difference between copy initialization and direct initialization in C++?
- Double brace initialization in Java
- Initialization vs Instantiation in C#
- Initialization of static variables in C

Advertisements