fork(1) download
  1. import sys
  2.  
  3. from itertools import zip_longest
  4.  
  5. def grouper(iterable, n, fillvalue=None):
  6. "Collect data into fixed-length chunks or blocks"
  7. # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
  8. args = [iter(iterable)] * n
  9. return zip_longest(*args, fillvalue=fillvalue)
  10.  
  11.  
  12.  
  13. m = 2
  14. file = sys.stdin
  15. output_file = sys.stdout
  16. for words in grouper(file, m, fillvalue=''):
  17. print(*map(str.strip, words), file=output_file)
Success #stdin #stdout 0.04s 9432KB
stdin
#word1
#word2
#word3
#word4
#...
stdout
#word1 #word2
#word3 #word4
#...