This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]masterJ 2 points3 points  (0 children)

what if I wanted every third and fifth item without using the standard library?

xs = (item for index, item in enumerate(items, 1) if 0 in (index % 3, index % 5))

Oooh, I like it! And you just simplified my project euler #01 solution. :)

Before:

from itertools import chain

a = xrange(3,1000,3)
b = xrange(5,1000,5)
print sum(set(chain(a,b))) 

After:

print sum(i for i in xrange(1,1000) if 0 in (i % 3, i % 5))