What would be the pythonic way to remove elements that are not uniques for certain keys?
Let's say one has a list of dicts such as:
[
{'a': 1, 'b': 'j'},
{'a': 2, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
The expected output would remove the second element, because the key b equals to j in more than one element. Thus:
[
{'a': 1, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
This is what I have tried:
input = [
{'a': 1, 'b': 'j'},
{'a': 2, 'b': 'j'},
{'a': 3, 'b': 'i'}
]
output = []
for input_element in input:
if not output:
output.append(input_element)
else:
for output_element in output:
if input_element['b'] != output_element['b']:
output.append(input_element)
Would the solution be simpler if that'd be a list of tuples, such as:
[(1, 'j'), (2, 'j'), (3, 'i')]
# to produce
[(1, 'j'), (3, 'i')]