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

All the possible ways to join lists that I could find

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) 2l = [ {'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) 5l = [ {'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) 7l = [ {'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) 8l = [ {'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) 9import 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'))) 0import 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'))) 1l = [ {'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) 8l = [ {'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) 9import 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'))) 4import 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'))) 1import 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'))) 6l = [ {'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) 9import 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'))) 8import 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'))) 1The 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)} 0l = [ {'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) 9The 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)} 2import 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'))) 1import 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'))) 6l = [ {'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) 9The 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)} 6The 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)} 8The 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)} 9INFO = [ {'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'} ]2INFO = [ {'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'} ]4l = [ {'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'} ]6INFO = [ {'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'} ]7INFO = [ {'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'} ]0students = [ {'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'} ]3students = [ {'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'} ]5l = [ {'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'} ]7students = [ {'mark': '65','grade': 'C'}, {'mark': '86','grade': 'A'}, {'mark': '73','grade': 'B'}, {'mark': '49','grade': 'D'}, {'mark': '91','grade': 'A'}, {'mark': '79','grade': 'B'} ]8students = [ {'mark': '65','grade': 'C'}, {'mark': '86','grade': 'A'}, {'mark': '73','grade': 'B'}, {'mark': '49','grade': 'D'}, {'mark': '91','grade': 'A'}, {'mark': '79','grade': 'B'} ]9A {'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'}0INFO = [ {'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'} ]0students = [ {'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)} 8The 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)} 9A {'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.groupby2l = [ {'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.groupby5l = [ {'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()2l = [ {'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()2l = [ {'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()1l = [ {'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) 02l = [ {'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()1l = [ {'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()1l = [ {'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) 12l = [ {'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()1l = [ {'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()1l = [ {'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) 22l = [ {'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()1l = [ {'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) 32l = [ {'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()1l = [ {'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()1l = [ {'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) 42l = [ {'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()1l = [ {'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) 52l = [ {'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()1l = [ {'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) 62l = [ {'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()6l = [ {'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'} ]3l = [ {'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()4l = [ {'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.groupby5l = [ {'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) 78l = [ {'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) 79l = [ {'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) 6l = [ {'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) 83students = [ {'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'} ]3The 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)} 8l = [ {'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'} ]3The 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)} 8The 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)} 9INFO = [ {'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'} ]7l = [ {'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.groupby2l = [ {'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) 99l = [ {'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'))) 02l = [ {'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.groupby9import 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()1import 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'))) 59l = [ {'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) 9import 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()1import 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'))) 33l = [ {'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

    Chủ đề