fork download
  1. import collections
  2.  
  3. result = collections.defaultdict(list)
  4. names = ['ABC', 'ACE', 'BED', 'BRT', 'CCD']
  5. for name in names:
  6. ch = name[0]
  7. if len(result[ch]) < 2:
  8. result[ch].append(name)
  9.  
  10. for ch0 in result:
  11. print('{}: {}'.format(ch0, result[ch0]))
Success #stdin #stdout 0.02s 8432KB
stdin
Standard input is empty
stdout
A: ['ABC', 'ACE']
C: ['CCD']
B: ['BED', 'BRT']