Search code examples
pythonlistdictionaryuniquekey-value

How to find unique key value from list of dictionary to create new list


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 the above list where producer and state are unique, so the new list will look like:

[
{'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 get all the unique producer names:

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

and then compare the producer for each asset with the producer_unique_list, only adding entries where (producer, state) is unique:

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 it looks like this logic is not working. How can I solve this?


Solution

  • 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)