from itertools import islice def gerador(): nome = "Stack Overflow" for char in nome: yield char def chunker(iterable, size): it = iter(iterable) return iter(lambda: list(islice(it, size)), []) for chunk in chunker(gerador(), 4): print(list(chunk)) for chunk in chunker(range(25), 4): print(list(chunk))
Standard input is empty
['S', 't', 'a', 'c'] ['k', ' ', 'O', 'v'] ['e', 'r', 'f', 'l'] ['o', 'w'] [0, 1, 2, 3] [4, 5, 6, 7] [8, 9, 10, 11] [12, 13, 14, 15] [16, 17, 18, 19] [20, 21, 22, 23] [24]