Open In App

Python | Boolean list initialization

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Many times in programming, we require to initialize a list with some initial values. In Dynamic programming, this is used more often and mostly the requirement is to initialize with a boolean 0 or 1. Let’s discuss certain ways in which this can be achieved.

Method #1: Using list comprehension 
This can easily be done with the naive method, hence, can also be converted into a compact version using list comprehension. This is the most basic way to perform this task.

Step-by-step approach :

  • The list comprehension [True for i in range(6)] is used to initialize the list with 6 boolean values all set to True.
  • The initialized list is stored in the variable res.
  • The final line uses the print() function to display the initialized list.
  • The output of the program will be the message “The True initialized list is:” followed by the initialized list of boolean values in the format [True, True, True, True, True, True].

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# to perform boolean list initializing
# using list comprehension
 
# using list comprehension
# to perform boolean list initializing
res =  [True for i in range(6)]
 
# printing result
print ("The True initialized list is : " +  str(res))


Output: 

The True initialized list is : [True, True, True, True, True, True]

 

Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

  
Method #2: Using * operator 
This can be done using the * operator in a comparatively more readable and compact way. We multiply the single list N no. of times to get the desired result.

Python3




# Python3 code to demonstrate
# to perform boolean list initializing
# using * operator
 
# using * operator
# to perform boolean list initializing
res =  [True] * 6
 
# printing result
print ("The True initialized list is : " +  str(res))


Output: 

The True initialized list is : [True, True, True, True, True, True]

 

  
Method #3 : Using bytearray() 
This method can be used to perform the list initialization, but this argument can only be extended to False value initializations. It doesn’t work when we require to initialize with the True values.

Python3




# Python3 code to demonstrate
# to perform boolean list initializing
# using bytearray()
 
# using bytearray()
# to perform boolean list initializing
res = list(bytearray(6))
 
# printing result
print ("The False initialized list is : " +  str(res))


Output: 

The False initialized list is : [0, 0, 0, 0, 0, 0]

 

Method #4: Using itertools

Here is a new approach using the itertools.repeat function:

Python3




import itertools
 
# Initialize a list of 6 True values
res = list(itertools.repeat(True, 6))
print(res)  # Output: [True, True, True, True, True, True]
 
# Initialize a list of 6 False values
res = list(itertools.repeat(False, 6))
print(res)  # Output: [False, False, False, False, False, False]
#This code is contributed by Edula Vinay Kumar Reddy


Output

[True, True, True, True, True, True]
[False, False, False, False, False, False]

This approach can be used to initialize a boolean list with either True or False values. It is a more concise and readable alternative to using a loop or list comprehension.

Note: The itertools.repeat function returns an iterator that will repeat the provided value a specified number of times. To create a list from this iterator, we can use the list function.

The time complexity of the itertools.repeat approach is O(n), where n is the number of elements in the list. This is because the list function needs to iterate over the entire iterator produced by itertools.repeat in order to create the final list.

The space complexity of this approach is also O(n), as the final list will have n elements.

Method #5: Using map() with lambda function

  1. Use the map() function to apply a lambda function to each element in a range of numbers.
  2. The lambda function takes each element and returns True.
  3. Convert the map object to a list to create a list of boolean values.
  4. Assign the list to a variable to store the result.

Python3




# Python3 code to demonstrate
# to perform boolean list initializing
# using map() with lambda function
 
# using map() with lambda function
# to perform boolean list initializing
res = list(map(lambda x: True, range(6)))
 
# printing result
print("The True initialized list is : " + str(res))


Output

The True initialized list is : [True, True, True, True, True, True]

Time complexity: O(n) – where n is the length of the range
Auxiliary space: O(n) – to store the list of boolean values



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads