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
res = [ True for i in range ( 6 )]
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
res = [ True ] * 6
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
res = list (bytearray( 6 ))
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
res = list (itertools.repeat( True , 6 ))
print (res)
res = list (itertools.repeat( False , 6 ))
print (res)
|
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
- Use the map() function to apply a lambda function to each element in a range of numbers.
- The lambda function takes each element and returns True.
- Convert the map object to a list to create a list of boolean values.
- Assign the list to a variable to store the result.
Python3
res = list ( map ( lambda x: True , range ( 6 )))
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Apr, 2023
Like Article
Save Article