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)
CodePudding user response:
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()]
CodePudding user response:
According the stated information in the question.
The first challenge is to union the two lists with a priority to the second list. A quick solution can be:
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'}
]
final_result = [
item for item in list1
if item['item_type'] not in
[item['item_type'] for item in list2]
] list2
print(final_result)
The second challenge is to union the two lists with a priority to the value
. A quick solution can be:
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'}
]
isNotIn = lambda item, L: len(
[el['item_type'] for el in L
if item['item_type'] == el['item_type']]
) == 0
isHigher = lambda item, L: len(
[el['item_type'] for el in L
if item['item_type'] == el['item_type'] and
item['value'] > el['value']]
) > 0
final_result = [el for el in list1 if (isNotIn(el, list2) or isHigher(el, list2))] \
[el for el in list2 if (isNotIn(el, list1) or isHigher(el, list1))]
print(final_result)