fork download
  1. from sys import stdin
  2. from itertools import groupby
  3. from operator import itemgetter, add
  4.  
  5. groups = stdin.read().split("\n\n")
  6. keys = groups[::2]
  7. values = groups[1::2]
  8. by_key = groupby(sorted(zip(keys, values)), itemgetter(0))
  9. res = {k: reduce(add, (s[1].split("\n") for s in v)) for k, v in by_key}
  10. print res
Success #stdin #stdout 0.01s 7740KB
stdin
key1

value1
value2
value3

key2

value1
value2
value3
value4

key3

value1

key4

value1
value2

key1

extra1
extra2
stdout
{'key3': ['value1'], 'key2': ['value1', 'value2', 'value3', 'value4'], 'key1': ['extra1', 'extra2', 'value1', 'value2', 'value3'], 'key4': ['value1', 'value2']}