# Python | List Initialization with alternate 0s and 1s

The initialization of list with a single number is a generic problem whose solution has been dealt many number of 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 which 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 elements’ occurrence. It takes the remainder of sum of counts of both occurrences of elements with particular occurrence of element for cycle computation.

 `# Python3 code to demonstrate  ` `# to perform cyclic initialization ` `# using list comprehension ` ` `  `# count of 1  ` `count_1 ``=` `4` ` `  `# count of 0 ` `count_0 ``=` `3` ` `  `# total length of list  ` `size ``=` `14` ` `  `# initializing list cyclically ` `# using list comprehension ` `test_list ``=`  `[ ``1` `if` `i ``%` `(count_1 ``+` `count_0) < count_1  ` `                 ``else` `0` `for` `i ``in` `range``(size) ] ` ` `  `# printing list after change ` `print` `(``"The list after initializing : "` `+` `str``(test_list)) `

Output:

```The list after initializing : [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
```

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 code to demonstrate  ` `# to perform cyclic initialization ` `# using itertools.cycle() + itertools.islice() ` `import` `itertools ` ` `  `# count of 1  ` `count_1 ``=` `4` ` `  `# count of 0 ` `count_0 ``=` `3` ` `  `# total length of list  ` `size ``=` `16` ` `  `# getting pattern ` `pattern ``=` `[``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 change ` `print` `(``"The list after initializing : "` `+` `str``(test_list)) `

Output:

```The list after initializing : [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1]
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.