Generating number Sequence Python

Question:

I want to generate a number sequence that repeats 2 consective numbers twice then skips a number and repeats the sequence wihin the range specified.

such as

0,0,1,1,3,3,4,4,6,6,7,7 and so forth.

what I have so far

numrange = 10
numsequence  = [i for i in range(numrange) for _ in range(2)]
numsequence

which produces

[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]

which is close but not quite what I want

Asked By: Spooked

||

Answers:

[i+i//2 for i in range(2*(numrange+1)//3) for _ in range(2)]

Is one on the many ways to do it. This one is probably the closest to your attempt.

Answered By: chrslg

You can iterate over numbers with the step 3:

[i + j for i in range(0, numrange, 3) for j in (0, 0, 1, 1)]

Output:

[0, 0, 1, 1, 3, 3, 4, 4, 6, 6, 7, 7, 9, 9, 10, 10]
Answered By: Yevhen Kuzmovych
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.