Prashant
Prashant

Reputation: 33

How to compose list comprehension including If Else conditions

What is a simple list comprehension (without using any new modules or dictionary) to get output as below:

[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]  

Here the number of 0s between each 1s increases from 0 to 10 times.

Without using list comprehension I got the output using simple for loops:

for i in range(12):
    if i==0:
        print(1, end=", ")
    else:
        for j in range(i):
            if i==j+1:
                print(1, end=", ")
            else:
                print(0, end=", ")

Just wanted to see how these loops can be converted to list comprehension

Upvotes: 3

Views: 143

Answers (3)

blhsing
blhsing

Reputation: 106445

The question asks for "a simple list comprehension" that outputs the list in question but the two other answers so far require an additional hardcoded list to be joined with the list produced by a list comprehension, which is not what the question asks for.

To do so with just a list comprehension you can increase the range of the inner loop by 1 so that you can produce the additional 1 when i is 0 and use an if clause to filter the output of an additional 0 for the rest of the iterations:

[1 if i == j else 0 for i in range(12) for j in range(i + 1) if j > 0 or i == 0]

Upvotes: 0

Nick
Nick

Reputation: 147146

You can generate the lists of 1 followed by an increasing number of 0's by using this list comprehension:

[[1] + [0] * i for i in range(n)]

For n = 4, this will produce:

[[1], [1, 0], [1, 0, 0], [1, 0, 0, 0]]

You can flatten that list by nesting it inside another comprehension, and then add the trailing 1:

res = [i for sub in [[1] + [0] * i for i in range(n)] for i in sub] + [1]

Again, for n = 4 this produces:

[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1]

If you can use libraries, you can use itertools.chain.from_iterable to flatten the list:

res = list(itertools.chain.from_iterable([1] + [0] * i for i in range(n))) + [1]

The output will be the same.

As pointed out by @KellyBundy in the comments, the need for the trailing 1 can be removed by changing the innermost comprehension in the above code to

[0] * i + [1] for i in range(-1, n)

This makes use of the fact that [0] * n = [] for n <= 0. For n = 4 this directly produces

[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1]

Upvotes: 6

Ralubrusto
Ralubrusto

Reputation: 1501

The answer with nested list comprehensions works pretty well. Here is an alternative using one single list comprehension:

N = 5  # Max number of 0s between 1s

ans = [1 if i == 0 else 0 for limit in range(N+2) for i in range (limit)] + [1]

The content of ans is as follows:

[1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]

Upvotes: 2

Related Questions