Using % 2
gives me the alternating sequence [0, 1, 0, 1, ...]
seq = []
for i in range(10):
e = i % 2
seq.append(e)
Is there a way to generate the sequence [0, 0, 1, 1, 0, 0, 1, 1,...]
by generating the element from inside the loop?
seq = []
for i in range(10):
e = the_solution(i)
seq.append(e)
e
twice. - Barmar(i >> 1) & 1
would give the desired value. - jasonharperitertools.cycle
to just repeat[0, 0, 1, 1]
however many times you want. E.g.from itertools import islice, cycle; list(islice(cycle([0, 0, 1, 1]), 25))
will give a list with the first 25 items of this sequence - Alexander