0

I have a list of dictionaries as follows.

[{'a' : 1, 'b' : 2, 'c' : 2},
 {'a' : 2, 'b' : 3, 'c' : 3},
 {'a' : 3, 'b' : 5, 'c' : 6},
 {'a' : 4, 'b' : 7, 'c' : 8},
 {'a' : 1, 'b' : 8, 'c' : 9},
 {'a' : 2, 'b' : 0, 'c' : 0},
 {'a' : 5, 'b' : 1, 'c' : 3},
 {'a' : 7, 'b' : 4, 'c' : 5}]

I want to create a dictionary of lists from above list which should be as follows.

{1 : [{'a' : 1, 'b' : 2, 'c' : 2}, {'a' : 1, 'b' : 8, 'c' : 9}]
 2 : [{'a' : 2, 'b' : 3, 'c' : 3}, {'a' : 2, 'b' : 0, 'c' : 0}]
 3 : [{'a' : 3, 'b' : 5, 'c' : 6}]
 4 : [{'a' : 4, 'b' : 7, 'c' : 8}]
 5 : [{'a' : 5, 'b' : 1, 'c' : 3}]
 7 : [{'a' : 7, 'b' : 4, 'c' : 5}]

Basically I want to pick one of the keys in dictionary say 'a', and create new dictionary with the values of that key (1, 2, 3, 4, 5, 7) as keys for new dictionary to be created, and values for new dictionary should be list of all the dictionaries containing that value as value for key 'a'.

I know the simplest approach is iterating over the list and build the required dictionary. I am just curious is there another way of doing it.

1
  • 4
    What is the approach you tried? Commented Aug 31, 2015 at 11:27

3 Answers 3

2

A collections.defaultdict will be the most efficient:

from collections import defaultdict

l = [{'a': 1, 'b': 2, 'c': 2},
 {'a': 2, 'b': 3, 'c': 3},
 {'a': 3, 'b': 5, 'c': 6},
 {'a': 4, 'b': 7, 'c': 8},
 {'a': 1, 'b': 8, 'c': 9},
 {'a': 2, 'b': 0, 'c': 0},
 {'a': 5, 'b': 1, 'c': 3},
 {'a': 7, 'b': 4, 'c': 5}]

dct = defaultdict(list)

for d in l:
    dct[d["a"]].append(d)

from pprint import pprint as pp
pp(dict(dct))

Output:

{1: [{'a': 1, 'b': 2, 'c': 2}, {'a': 1, 'b': 8, 'c': 9}],
 2: [{'a': 2, 'b': 3, 'c': 3}, {'a': 2, 'b': 0, 'c': 0}],
 3: [{'a': 3, 'b': 5, 'c': 6}],
 4: [{'a': 4, 'b': 7, 'c': 8}],
 5: [{'a': 5, 'b': 1, 'c': 3}],
 7: [{'a': 7, 'b': 4, 'c': 5}]}
Sign up to request clarification or add additional context in comments.

Comments

1

Normal dictionary with setdefault method can be used

Code:

data=[{'a' : 1, 'b' : 2, 'c' : 2},
{'a' : 2, 'b' : 3, 'c' : 3},
{'a' : 3, 'b' : 5, 'c' : 6},
{'a' : 4, 'b' : 7, 'c' : 8},
{'a' : 1, 'b' : 8, 'c' : 9},
{'a' : 2, 'b' : 0, 'c' : 0},
{'a' : 5, 'b' : 1, 'c' : 3},
{'a' : 7, 'b' : 4, 'c' : 5}]

dictionary_list={}
for row in data:
    dictionary_list.setdefault(row["a"],[]).append(row)
print dictionary_list

Output:

{1: [{'a': 1, 'c': 2, 'b': 2}, {'a': 1, 'c': 9, 'b': 8}],
2: [{'a': 2, 'c': 3, 'b': 3}, {'a': 2, 'c': 0, 'b': 0}],
3: [{'a': 3, 'c': 6, 'b': 5}],
4: [{'a': 4, 'c': 8, 'b': 7}],
5: [{'a': 5, 'c': 3, 'b': 1}],
7: [{'a': 7, 'c': 5, 'b': 4}]}

Comments

0

You can do it in following way

mylist = [
   {'a' : 1, 'b' : 2, 'c' : 2},
   {'a' : 2, 'b' : 3, 'c' : 3},
   {'a' : 3, 'b' : 5, 'c' : 6},
   {'a' : 4, 'b' : 7, 'c' : 8},
   {'a' : 1, 'b' : 8, 'c' : 9},
   {'a' : 2, 'b' : 0, 'c' : 0},
   {'a' : 5, 'b' : 1, 'c' : 3},
   {'a' : 7, 'b' : 4, 'c' : 5}
]

def get_dict(mylist, required_key):
    result_dict = {}

    for mydict in mylist:
        result_dict.setdefault(mydict[required_key], [])
        result_dict[mydict[required_key]].append(mydict)

    return result_dict

result_dict = get_dict(mylist, required_key = 'a')
print(result_dict)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.