Say I have a list, l, which contains [5, 10, 15, 16, 20]. I want to get the first elements that are all divisible by 5 (so the first three elements), without the last 20. How do I do this? I've done:
done = False
i = 0
while not done and i < len(l):
if l[i] % 5 == 0:
answer.append(source[i])
else:
done = True
i += 1
However, this seems inefficient. Is there any better way to do this?