fork download
  1. from itertools import islice
  2.  
  3. def gerador():
  4. nome = "Stack Overflow"
  5. for char in nome:
  6. yield char
  7.  
  8. def chunker(iterable, size):
  9. it = iter(iterable)
  10. return iter(lambda: list(islice(it, size)), [])
  11.  
  12. for chunk in chunker(gerador(), 4):
  13. print(list(chunk))
  14.  
  15. for chunk in chunker(range(25), 4):
  16. print(list(chunk))
Success #stdin #stdout 0.03s 9308KB
stdin
Standard input is empty
stdout
['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]