Home > Software engineering >  How to find unique key value from list of dictionary to create new list
How to find unique key value from list of dictionary to create new list

Time:12-14

I have below list :

asset_list = 
[ 
{'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-1', 'state': 'Alabama', 'rid': 103, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}, 
{'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-2', 'state': 'Alaska', 'rid': 104, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}, 
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama', 'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama', 'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'}, 
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-4', 'state': 'Arizona', 'rid': 106, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'}, 
{'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-5', 'state': 'California', 'rid': 107, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'}, 
{'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-7', 'state': 'Alabama', 'rid': 108, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}

]

I have to create another list from above list where producer and state are unique, so new list will look like below:

[
{'producer': 'HIK', 'state': 'Alabama'},
{'producer': 'HIK', 'state': 'Alaska'},
{'producer': 'TF Inc.', 'state': 'Alabama'},
{'producer': 'TF Inc.', 'state': 'Arizona'},
{'producer': 'TF Inc.', 'state': 'California'},
]

To solve this, I first got all the unique producer name:

producer_unique_list = []
    for producer in asset_list:
        if producer['producer'] not in producer_unique_list:
            producer_unique_list.append(producer['producer'])

and then one by in asset list I am comparing it with producer_unique_list and only adding state

producer_list = []
    run_once = True
    for ass in asset_list:
        p_d = dict()
        if run_once:
            p_d['producer'] = ass['producer']
            p_d['state'] = ass['state']
            producer_list.append(p_d)
            run_once = False
        else:
            for y in producer_list:
                if ass['producer'] != y['producer'] or ass['state'] != y['state']:
                    p_d = dict()
                    p_d['producer'] = ass['producer']
                    p_d['state'] = ass['state']
                    producer_list.append(y)

But looks like this logic is not working. Can anyone please suggest some good way to solve this. Thanks

CodePudding user response:

You can start by building a set of tuples based on producer and state. Using a set will handle the duplication issue. The you can build a list of dictionaries based on the set as follows:

asset_list = [
    {'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-1', 'state': 'Alabama',
        'rid': 103, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'},
    {'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-2', 'state': 'Alaska',
        'rid': 104, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama',
        'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-3', 'state': 'Alabama',
        'rid': 105, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-4', 'state': 'Arizona',
        'rid': 106, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 64, 'client_id': '232-00000008', 'prem_id': 'PID-5', 'state': 'California',
        'rid': 107, 'total_premid': 0, 'total_site': 0, 'producer': 'TF Inc.'},
    {'cid': 60, 'client_id': '232-00000004', 'prem_id': 'PID-7', 'state': 'Alabama',
        'rid': 108, 'total_premid': 0, 'total_site': 0, 'producer': 'HIK'}

]
s = set()
for a in asset_list:
    s.add((a['producer'], a['state']))
out = [{'producer': i[0], 'state': i[1]} for i in s]
print(out)

CodePudding user response:

With following code I was able to get desired result:

unique_values_set = set([tuple((entry["producer"], entry["state"])) for entry in asset_list])

unique_dict_list = [{'producer': value[0], 'state': value[1]} for value in unique_values_set]
  • Related