ashok ashok - 2 months ago 6
Python Question

How do I pick one dict inside a list of dictionaries that are differed by one value?

I have a list of dictionaries like this:

xyz =[ {"key1":"1","key2":"2","key3":"x_1"},{"key1":"1","key2":"2","key3":"x_2"},{"key1":"1","key2":"2","key3":"x_3"},{"key1":"5","key2":"6","key3":"y_1"},{"key1":"5","key2":"6","key3":"y_2"},{"key1":"5","key2":"6","key3":"y_3"}]


I am trying to select first dict for every unique 'key1' value. For the above dict I am expecting an output as:

xyz=[{"key1":"1","key2":"2","key3":"x_1"},{"key1":"5","key2":"6","key3":"y_1"},]


I tried the following code:

dictout = [dict(sample) for sample in set(tuple(item.items()) for item in xyz)]


But I ended up getting:

[{'key3': 'x_1', 'key2': '2', 'key1': '1'}, {'key3': 'x_3', 'key2': '2', 'key1': '1'}, {'key3': 'y_2', 'key2': '6', 'key1': '5'}, {'key3': 'x_2', 'key2': '2', 'key1': '1'}, {'key3': 'y_1', 'key2': '6', 'key1': '5'}, {'key3': 'y_3', 'key2': '6', 'key1': '5'}]


I am not able to construct unique dicts based on the value of "key1".

It would be great if someone gives me an idea to approach.

Thank you.

Answer

You can use groupby from the itertools library:

import itertools

xyz =[ {"key1":"1","key2":"2","key3":"x_1"},{"key1":"1","key2":"2","key3":"x_2"},
       {"key1":"1","key2":"2","key3":"x_3"},{"key1":"5","key2":"6","key3":"y_1"},
       {"key1":"5","key2":"6","key3":"y_2"},{"key1":"5","key2":"6","key3":"y_3"}]

result = []
for key, value in itertools.groupby(xyz, lambda x: x["key1"]):
    result.append(list(value))

print([x[0] for x in result])

Output:

[{'key1': '1', 'key2': '2', 'key3': 'x_1'}, {'key1': '5', 'key2': '6', 'key3': 'y_1'}]

Or even with list comprehension:

result = [list(value) for key, value in itertools.groupby(xyz, lambda x: x["key1"])]

You can also use itemgetter from the operator library:

import operator
import itertools

result = [list(value) for key, value in itertools.groupby(xyz, operator.itemgetter("key1")]

You can also use another dictionary too:

d = {}
for key, value in itertools.groupby(xyz, lambda x: x["key1"]):
    d[key] = list(value)

print([value[0] for key, value in sorted(d.items())])
Comments