Filter values from list of dicts by list of values
Question:
I have a list of dictionaries like this:
data = [{'key1':'a', 'key2':'100', 'varying key/value pairs'}, {'key1':'b', 'key2':'50', 'varying key/value pairs'}, {'key1':'a', 'key2':'100', 'varying key/value pairs'}, {'key1':'b', 'key2':'50', 'varying key/value pairs'}, {'key1':'c', 'key2':'150', 'varying key/value pairs'},...]
And I have a list of filters like this (which can vary by case in content and length):
filter = ['a', 'b']
And I want to make create new new dictionary, but eliminate double counting like this:
new_data = [{'key1':'a', 'key2':'100'}, {'key1':'b', 'key2':'50'}]
Any ideas how to achieve this?
Answers:
you can first convert the list to a set and then convert it back to list. Note that the order of the dict in new list may not be the same as the order as before, as sets are unordered by nature.
unique_keys = {d['key1']: d['key2'] for d in data if d['key1'] in filters}
new_data = [{'key1': k, 'key2': v} for k, v in unique_keys.items()]
What about using a set
to select the valid keys and a temporary dictionary (as a dictionary comprehension) to remove the duplicates?
S = {'a', 'b'}
new_data = list({d['key1']: d for d in data if d['key1'] in S}.values())
Output:
[{'key1': 'a', 'key2': '100'}, {'key1': 'b', 'key2': '50'}]
I have a list of dictionaries like this:
data = [{'key1':'a', 'key2':'100', 'varying key/value pairs'}, {'key1':'b', 'key2':'50', 'varying key/value pairs'}, {'key1':'a', 'key2':'100', 'varying key/value pairs'}, {'key1':'b', 'key2':'50', 'varying key/value pairs'}, {'key1':'c', 'key2':'150', 'varying key/value pairs'},...]
And I have a list of filters like this (which can vary by case in content and length):
filter = ['a', 'b']
And I want to make create new new dictionary, but eliminate double counting like this:
new_data = [{'key1':'a', 'key2':'100'}, {'key1':'b', 'key2':'50'}]
Any ideas how to achieve this?
you can first convert the list to a set and then convert it back to list. Note that the order of the dict in new list may not be the same as the order as before, as sets are unordered by nature.
unique_keys = {d['key1']: d['key2'] for d in data if d['key1'] in filters}
new_data = [{'key1': k, 'key2': v} for k, v in unique_keys.items()]
What about using a set
to select the valid keys and a temporary dictionary (as a dictionary comprehension) to remove the duplicates?
S = {'a', 'b'}
new_data = list({d['key1']: d for d in data if d['key1'] in S}.values())
Output:
[{'key1': 'a', 'key2': '100'}, {'key1': 'b', 'key2': '50'}]