trex
trex

Reputation: 4037

Create unique dictionaries for two keys in a list of dictionaries

I am getting a list of dictionaries in the following format from an API, :

eg.

xlist =[
    { "id":1, "day":2, name:"abc", ... },
    { "id":1, "day":3, name:"abc", ... },
    { "id":1, "day":2, name:"xyz", ... },
    { "id":1, "day":3, name:"xyz", ... },...
  ]   

So, to store/optimize the queries in to the DB I have to convert them in this format.

What I am doing :

 names = list(set([ v['name'] for v in xlist])) #-- GET UNIQUE NAMES
 templist =  [{k:(names if k == 'name' else v) \
           for k, v in obj.items()} for obj in xlist] #-- Append Unique names
 unique_xlist= {v['day']:v for v in templist}.values() #- find unique dicts

I don't think this is very efficient, am using 3 loops just to find unique dicts by day.

Upvotes: 2

Views: 1236

Answers (2)

Eugene Yarmash
Eugene Yarmash

Reputation: 149756

You could use itertools.groupby:

from itertools import groupby

xlist.sort(key=lambda x: (x["id"], x["day"], x["name"]))  # or use sorted()
unique_xlist = []

for k, g in groupby(xlist, lambda x: (x["id"], x["day"])):
    unique_xlist.append({"id": k[0], "day": k[1], "name": [i["name"] for i in g]})

Upvotes: 5

deceze
deceze

Reputation: 522024

Simply use the values that makes an item unique as keys to a dictionary:

grouped = {}
for x in xlist:
    key = (x['id'], x['day'])
    try:
        grouped[key]['name'].append(x['name'])
    except KeyError:
        grouped[key] = x
        grouped[key]['name'] = [x['name']]

You can listify this again afterwards if necessary.

Upvotes: 1

Related Questions