How to create a priority list of dictionary

Viewed 538
  • list is below
  • preference dictionary is below
  • if all the keys and values except type will be same then ..
  • Need to compare type in each list which is highest order in preference dictionary
  • Output is list of dictionary which type is highest order
list_ = [
  {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Owner"
  },
      {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Manager"
  },
{
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Employ"
  },
{
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Manager"
  }
]

A preference dictionary = {'Owner': 1, 'Manager':2, 'employ':3, 'HR': 4 }

My expected output dictionary below

[{'id': '11', 'name': 'son', 'email': 'n@network.com', 'type': 'Owner'},
{'id':'21','name': 'abc','email': 'abc@network.com','type': 'Manager'}]

new_list = []
for each in list_:
    if each['type'] in priority.keys():
        if each['id'] not in new_list:
            new_list.append(each)
4 Answers

You can simply do src.sort(key = lambda x : preference[x["type"]]) and your list will be sorted.

This solution groups all the elements by id, and sorts the groups according to the preference (so that the Owner is first and HR is last) and then just picks the first from each group:

from collections import defaultdict

src = [
  {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Owner"
  },
      {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Manager"
  },
{
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Employ"
  },
{
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Manager"
  }
]
preference = {'Owner': 1, 'Manager':2, 'Employ':3, 'HR': 4 }

d = defaultdict(list)
# group all the records by id
for item in src:
    d[item['id']].append(item)

# sort each group by the preference
for item in d.values():
    item.sort(key=lambda x: preference[x['type']])

# select only the first from each group
result = [item[0] for item in d.values()]

print(result)

Output:

[{'id': '11', 'name': 'son', 'email': 'n@network.com', 'type': 'Owner'}, {'id': '21', 'name': 'abc', 'email': 'abc@network.com', 'type': 'Manager'}]

You could create a priority queue:

from queue import PriorityQueue

priority = {'Owner': 1, 'Manager':2, 'employ':3, 'HR': 4 }

q = PriorityQueue()
for elem in list_:
    p = priority[elem['type']]
    q.put((p, id(elem), elem))

Or you could also sort a list based on the type with:

priority_list = sorted(list_, key=lambda x: priority[x['type']], reverse=True)

Well here's my shot!

It isn't beautiful but it seems to work.

list_ = [
  {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Owner"
  },
      {
    "id": "11",
    "name": "son",
    "email": "n@network.com",
    "type": "Manager"
  },
{
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Employ"
  },
{
    "id": "21",
    "name": "abc",
    "email": "abc@network.com",
    "type": "Manager"
  }
]

new = dict( Owner = 0, Manager = 0, Employ = 0, HR = 0 )

for a in list_ :
    type_ = a[ 'type' ]
    if type_ == 'Owner':
        new[ 'Owner' ] += 1
    if type_ == 'Manager':
        new[ 'Manager' ] += 1
    if type_ in [ 'Employ', 'Manager' ]:
        new[ 'Employ' ] += 1
    new[ 'HR' ] += 1

print( new )
Related