Search code examples
pythonlistdictionarylambdalist-comprehension

Write optimized code which takes following two list of dictionaries as input and gives output as final_result.I have solved it but I want to optimized


Inputs:

list1 = [{'item_type':1,'value':55, 'title':'abc'},{'item_type':2,'value':43, 'title':'def'},{'item_type':3,'value':35, 'title':'ghi'}]

list2 = [{'item_type':2,'value':13, 'title':'jkl'},{'item_type':3,'value':85, 'title':'mno'}]

Challange1: If the item_type is present in list2 then that should take the priority.

expected result:

final_result = [{'item_type':1,'value':55, 'title':'abc'},{'item_type':2,'value':13, 'title':'jkl'},{'item_type':3,'value':85, 'title':'mno'}]

Challange2: It should merge list1 and list2 based on unique item_type and keep the higher 'value' dictionary.

expected result:

final_result = [{'item_type':1,'value':55, 'title':'abc'},{'item_type':2,'value':43, 'title':'def'},{'item_type':3,'value':85, 'title':'mno'}]
. 

I have solved both the challenges but I want to optimized this code using list comprehensive, lambda .. please help me...

this is my code

Challange1:

final_result = []
list1 = [{'item_type':1,'value':55, 'title':'abc'},{'item_type':2,'value':43, 'title':'def'},{'item_type':3,'value':35, 'title':'ghi'}]
list2 = [{'item_type':2,'value':13, 'title':'jkl'},{'item_type':3,'value':85, 'title':'mno'}]

for i in range(len(list1)):
    for j in range(len(list2)):
        if list1[i]['item_type'] == list2[j]['item_type']:
            if list1[i]['item_type'] < list2[j]['item_type']:
                final_result.append(list1[i])
            else:
                final_result.append(list2[j])
            break
    else:
        final_result.append(list1[i])
print(final_result)

Challange2:

final_result = []
list1 = [{'item_type':1,'value':55, 'title':'abc'},{'item_type':2,'value':43, 'title':'def'},{'item_type':3,'value':35, 'title':'ghi'}]
list2 = [{'item_type':2,'value':13, 'title':'jkl'},{'item_type':3,'value':85, 'title':'mno'}]

for i in range(len(list1)):
    for j in range(len(list2)):
        if list1[i]['item_type'] == list2[j]['item_type']:
            if list1[i]['value'] > list2[j]['value']:
                final_result.append(list1[i])
            else:
                final_result.append(list2[j])
            break
    else:
        final_result.append(list1[i])
print(final_result)

Solution

  • So you want to use list (or other) comprehension.

    From Python 3.7, dictionaries are officially ordered. You can (1) concatenate the lists and sort the items if necessary, (2) put the items into a dictionary with item_type as their keys, and (3) convert values in the dictionary back to the list, to get the desired results.

    Challange1:

    final_result = [*{item['item_type']: item for item in list1 + list2}.values()]
    

    Challange2:

    final_result = [*{item['item_type']: item for item in sorted(list1 + list2, key=lambda item: (item['item_type'], item['value']))}.values()]