from itertools import groupby
INFO = [
{'employee': 'XYZ_1', 'company': 'ABC_1'},
{'employee': 'XYZ_2', 'company': 'ABC_2'},
{'employee': 'XYZ_3', 'company': 'ABC_3'},
{'employee': 'XYZ_4', 'company': 'ABC_3'},
{'employee': 'XYZ_5', 'company': 'ABC_2'},
{'employee': 'XYZ_6', 'company': 'ABC_3'},
{'employee': 'XYZ_7', 'company': 'ABC_1'},
{'employee': 'XYZ_8', 'company': 'ABC_2'},
{'employee': 'XYZ_9', 'company': 'ABC_1'}
]
def key_func(k):
return k['company']
INFO = sorted(INFO, key=key_func)
for key, value in groupby(INFO, key_func):
print(key)
print(list(value))