I have two lists:
list1 = [ {'sth': 13, 'important_key1': 'AA', 'important_key2': '3'}, {'oh!': 14, 'important_key1': 'FF', 'important_key2': '4'}, {'sth_else': 'abc', 'important_key1': 'ZZ', 'important_key2': '5'}]
list2 = [ {'why-not': 'tAk', 'important_key1': 'GG', 'important_key2': '4'}, {'hmmm': 'no', 'important_key1': 'AA', 'important_key2': '3'}]
I want to return a list with objects only from list1 but if the same important_key1
and important_key2
is in any element in list2 I want this element from list2.
So the output should be:
[ {'hmmm': 'no', 'important_key1': 'AA', 'important_key2': '3'}, {'oh!': 14, 'important_key1': 'FF', 'important_key2': '4'}, {'sth_else': 'abc', 'important_key1': 'ZZ', 'important_key2': '5'}]
It is not complicated to do it by two or three loops but I wonder whether there is a simple way by using list comprehensions or something like that.
This is the "normal" way:
list1 = [ {'sth': 13, 'important_key1': 'AA', 'important_key2': '3'}, {'oh!': 14, 'important_key1': 'FF', 'important_key2': '4'}]
list2 = [ {'hmmm': 'no', 'important_key1': 'AA', 'important_key2': '3'}, {'why-not': 'tAk', 'important_key1': 'GG', 'important_key2': '4'}]
final_list = []
for element in list1:
there_was_in_list2 = False
for another_element in list2:
if element['important_key1'] == another_element['important_key1'] and element['important_key2'] == another_element['important_key2']:
final_list.append(another_element)
there_was_in_list2 = True
break
if not there_was_in_list2:
final_list.append(element)
print(final_list)
is there any Pythonic way to do that?