Hướng dẫn dùng want list python

All the possible ways to join lists that I could find

Show
import itertools

A = [1,3,5,7,9] + [2,4,6,8,10]

B = [1,3,5,7,9]
B.append([2,4,6,8,10])

C = [1,3,5,7,9]
C.extend([2,4,6,8,10])

D = list(zip([1,3,5,7,9],[2,4,6,8,10]))
E = [1,3,5,7,9]+[2,4,6,8,10]
F = list(set([1,3,5,7,9] + [2,4,6,8,10]))

G = []
for a in itertools.chain([1,3,5,7,9], [2,4,6,8,10]):
    G.append(a)


print("A: " + str(A))
print("B: " + str(B))
print("C: " + str(C))
print("D: " + str(D))
print("E: " + str(E))
print("F: " + str(F))
print("G: " + str(G))

Output

A: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
B: [1, 3, 5, 7, 9, [2, 4, 6, 8, 10]]
C: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
D: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
E: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
F: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
G: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]

I want to generate a dictionary from a list of dictionaries, grouping list items by the value of some key, such as:

Nội dung chính Show

  • Itertools.groupby()
  • How to groupby list of dictionary in Python?
  • How do I convert a list to a dictionary in Python?
  • How do you group dictionary using keys?
  • Can we combine dictionaries if so how?

input_list = [
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
]
output_dict = {
        'pipo': [
             {'a': 'pipo', 'b': 'titi'}, 
             {'a': 'pipo', 'b': 'toto'}
         ],
         'tata': [
             {'a': 'tata', 'b': 'foo'},
             {'a': 'tata', 'b': 'bar'}
         ]
}

So far I've found two ways of doing this. The first simply iterates over the list, create sublists in the dict for each key value and append elements matching these keys to the sublist:

l = [ 
    {'a':'tata', 'b': 'foo'},
    {'a':'pipo', 'b': 'titi'},
    {'a':'pipo', 'b': 'toto'},
    {'a':'tata', 'b': 'bar'}
    ]

res = {}

for e in l:
    res[e['a']] = res.get(e['a'], []) 
    res[e['a']].append(e)

And another using itertools.groupby:

import itertools
from operator import itemgetter

l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
]

l = sorted(l, key=itemgetter('a'))
res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))

I wonder which alternative is the most efficient?

Is there any more pythonic/concise or better performing way of achieving this?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while working with data, we can be encountered with a situation in which we have list of list and we need to group it’s 2nd index with the common initial element in lists. Let’s discuss way in which this problem can be solved.

    Method : Using defaultdict() + loop + dict()

    The defaultdict can be used to initialize the group elements and loop can be used to group the values together and conversion to dictionary can be done using dict().

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    2
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    3
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    4

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    5
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    7
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    8
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    0
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    8
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    4
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    1
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    6
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    8
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    1
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    0
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    2
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    1
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    6
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    6
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    7

    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    8
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    9
    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'}
    ]
    0
    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'}
    ]
    1
    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'}
    ]
    2
    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'}
    ]
    3

    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'}
    ]
    4
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6
    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'}
    ]
    6
    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'}
    ]
    7
    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'}
    ]
    8

    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'}
    ]
    9
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    0
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    1
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    2

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    4

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    5
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    7
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    8
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    9
    A
    {'mark': '86', 'grade': 'A'}
    {'mark': '91', 'grade': 'A'}
    B
    {'mark': '73', 'grade': 'B'}
    {'mark': '79', 'grade': 'B'}
    C
    {'mark': '65', 'grade': 'C'}
    D
    {'mark': '49', 'grade': 'D'}
    0
    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'}
    ]
    9
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    0
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    1
    A
    {'mark': '86', 'grade': 'A'}
    {'mark': '91', 'grade': 'A'}
    B
    {'mark': '73', 'grade': 'B'}
    {'mark': '79', 'grade': 'B'}
    C
    {'mark': '65', 'grade': 'C'}
    D
    {'mark': '49', 'grade': 'D'}
    4

    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    8
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    9
    A
    {'mark': '86', 'grade': 'A'}
    {'mark': '91', 'grade': 'A'}
    B
    {'mark': '73', 'grade': 'B'}
    {'mark': '79', 'grade': 'B'}
    C
    {'mark': '65', 'grade': 'C'}
    D
    {'mark': '49', 'grade': 'D'}
    7
    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'}
    ]
    1
    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'}
    ]
    2itertools.groupby0

    Output :

    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    

    Group List of Dictionary Data by Particular Key in Python can be done using itertools.groupby() method.

    Itertools.groupby()

    This method calculates the keys for each element present in iterable. It returns key and iterable of grouped items.

    Syntax: itertools.groupby(iterable, key_func)

    Parameters:

    • iterable: Iterable can be of any kind (list, tuple, dictionary).
    • key_func: A function that calculates keys for each element present in iterable.

    Return type: It returns consecutive keys and groups from the iterable. If the key function is not specified or is None, key defaults to an identity function and returns the element unchanged.

    Let’s see the examples: Example 1: Suppose we have list of dictionary of employee and company.

    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'}
    ]

    Now we need to display all the data group by the ‘company’ key name.

    Code: 

    Python3

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    1 itertools.groupby2
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    3 itertools.groupby4

    itertools.groupby5

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6 itertools.groupby7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1defaultdict()2
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1defaultdict()6defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1dict()2
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1dict()6defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    02
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    06defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    12
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    06defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    22
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1dict()6defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    32
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    06defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    42
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1defaultdict()6defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    52
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1dict()6defaultdict()7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9defaultdict()0defaultdict()1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    62
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9defaultdict()4defaultdict()1defaultdict()6
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    67

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    68

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    69
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    70

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    72
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    73defaultdict()4
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    68

    itertools.groupby5

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    78
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    79
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    81

    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'}
    ]
    9
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    83
    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    85

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    8
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    88

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    8
    The original list is : [['Gfg', 1], ['Gfg', 2], ['is', 3], ['best', 4], ['is', 5]]
    The grouped dictionary is :  {'Gfg': (1, 2), 'best': (4, ), 'is': (3, 5)}
    
    9
    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'}
    ]
    7
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    93

    Output:

    ABC_1 [{’employee’: ‘XYZ_1’, ‘company’: ‘ABC_1′}, {’employee’: ‘XYZ_7’, ‘company’: ‘ABC_1′}, {’employee’: ‘XYZ_9’, ‘company’: ‘ABC_1′}] ABC_2 [{’employee’: ‘XYZ_2’, ‘company’: ‘ABC_2′}, {’employee’: ‘XYZ_5’, ‘company’: ‘ABC_2′}, {’employee’: ‘XYZ_8’, ‘company’: ‘ABC_2′}] ABC_3 [{’employee’: ‘XYZ_3’, ‘company’: ‘ABC_3′}, {’employee’: ‘XYZ_4’, ‘company’: ‘ABC_3′}, {’employee’: ‘XYZ_6’, ‘company’: ‘ABC_3’}]

    Example 2: Suppose we have list of dictionary of student grades and marks.

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]

    Now we need to display all the data group by the ‘grade’ key.

    Code: 

    Python3

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    1 itertools.groupby2
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    3 itertools.groupby4

    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    1
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    99
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    3
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    01

    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    02
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    6 itertools.groupby7

    students = [
        {'mark': '65','grade': 'C'},
        {'mark': '86','grade': 'A'},
        {'mark': '73','grade': 'B'},
        {'mark': '49','grade': 'D'},
        {'mark': '91','grade': 'A'},
        {'mark': '79','grade': 'B'}
    ]
    3itertools.groupby9
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    07defaultdict()1
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    59
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    9
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    11defaultdict()1
    import itertools
    from operator import itemgetter
    
    l = [ 
            {'a':'tata', 'b': 'foo'},
            {'a':'pipo', 'b': 'titi'},
            {'a':'pipo', 'b': 'toto'},
            {'a':'tata', 'b': 'bar'}
    ]
    
    l = sorted(l, key=itemgetter('a'))
    res = dict((k, list(g)) for k, g in itertools.groupby(l, key=itemgetter('a')))
    
    33
    l = [ 
        {'a':'tata', 'b': 'foo'},
        {'a':'pipo', 'b': 'titi'},
        {'a':'pipo', 'b': 'toto'},
        {'a':'tata', 'b': 'bar'}
        ]
    
    res = {}
    
    for e in l:
        res[e['a']] = res.get(e['a'], []) 
        res[e['a']].append(e)
    
    67