FYI, itertools.compress() is very useful in conjunction with itertools.cycle() to pick out elements following a periodic pattern of indices. For example,
# Elements at even indices. >>> list(compress(range(20), cycle([1, 0]))) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] # Or at odd ones. >>> list(compress(range(20), cycle([0, 1]))) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] # Pick every third. >>> list(compress(range(20), cycle([0, 0, 1]))) [2, 5, 8, 11, 14, 17] # Omit every third. >>> list(compress(range(20), cycle([1, 1, 0]))) [0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19] For arguments that are re-iteraerable, there are several ways to get the proposed semantics, including just passing the argument twice to compress(): >>> a = [None, "", "-filove-python", "CFLAGS=-O3"] >>> " ".join(compress(a, a)) '-filove-python CFLAGS=-O3' _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/XGCVW3LNEU2FNMB7JYTIQENK4KGHV4LZ/ Code of Conduct: http://python.org/psf/codeofconduct/