Python | List Initialization with alternate 0s and 1s
The initialization of a list with a single number is a generic problem whose solution has been dealt with many times. But sometimes we require to initialize the list with elements alternatively repeating K no. of times. This has use cases in M.L. or A. I algorithms that require presetting of data in lists. Let’s discuss certain ways in which this problem is solved.
Method #1: Using list comprehension In this method, we insert elements in the list alternatively for the specific number of times of each element’s occurrence. It takes the remainder of the sum of counts of both occurrences of elements with the particular occurrence of elements for cycle computation.
Python3
# Python3 code to demonstrate# to perform cyclic initialization# using list comprehension# count of 1count_1 = 4# count of 0count_0 = 3# total length of listsize = 14# initializing list cyclically# using list comprehensiontest_list = [1 if i % (count_1 + count_0) < count_1 else 0 for i in range(size)]# printing list after changeprint("The list after initializing : " + str(test_list)) |
The list after initializing : [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
Time complexity: O(n), where n is the size of the list.
Auxiliary space: O(n), for the list test_list.
Method #2 : Using itertools.cycle() + itertools.islice() This is the most pythonic way in which we can perform the cyclic initialization. Slice each of the part of list into the allotted element size in a cyclic manner using cycle function.
Python3
# Python3 code to demonstrate# to perform cyclic initialization# using itertools.cycle() + itertools.islice()import itertools# count of 1count_1 = 4# count of 0count_0 = 3# total length of listsize = 16# getting patternpattern = [1] * count_1 + [0] * count_0# initializing list cyclically# using itertools.cycle() + itertools.islice()test_list = list(itertools.islice(itertools.cycle(pattern), size))# printing list after changeprint("The list after initializing : " + str(test_list)) |
The list after initializing : [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1]
Method #3 : Using *,extend() and slicing
Python3
# Python3 code to demonstrate# to perform cyclic initialization# using list comprehension# count of 1count_1 = 4# count of 0count_0 = 3# total length of listsize = 14# initializing list cyclicallyx = [1]*count_1y = [0]*count_0res = []while(len(res) <= size): res.extend(x) res.extend(y)res = res[:size]# printing list after changeprint("The list after initializing : " + str(res)) |
The list after initializing : [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
Method #4 : Using itertools.repeat():
To use the repeat() function from the itertools module to initialize a list with alternate 0s and 1s, you can use the following approach:
Python3
from itertools import repeat# count of 1scount_1 = 4# count of 0scount_0 = 3# total length of listsize = 14# initialize list with alternate 0s and 1stest_list = list(repeat(1, count_1)) + list(repeat(0, count_0))# repeat list until it has the desired sizetest_list = test_list * (size // len(test_list)) + test_list[:size % len(test_list)]# print list after initializationprint("The list after initializing:", test_list)#This code is contributed by Edula Vinay Kumar Reddy |
The list after initializing: [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
Time complexity: O(n), where n is the size of the final list. This is because the time taken to create the list is directly proportional to the size of the list.
Auxiliary space: O(n), because the size of the final list is directly proportional to the size of the input.






Please Login to comment...