3

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

3 Answers 3

6

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]
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the quick response but the last entry has to be 1
@Prashant no worries - I hope you noticed my edit as I realised my original code did not add the trailing 1 to the output.
Yeah, just edited my last comment. But its helpful. I'll try to tweak it to work.
Small variation: list(itertools.chain.from_iterable([0] * i + [1] for i in range(-1, n)))
@KellyBundy nice! I love the use of [0]*-1 although I do wonder if it might be something that breaks in the future. If you don't mind I'll add that to the answer.
|
2

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]

1 Comment

This still requires a hardcoded list to be joined with the list produced by a list comprehension, which is not really what the question asks for.
0

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.