0

I have list of dict where i want to extract specific dicts if they contain certain key-value pairs Ex of list of dict

[
{'value': 'def', 'key': 'abc'},
{'value': 'xyz', 'key': 'mnp'},
{'value': '456', 'key': '123'},
{'value': '234', 'key': '789'}
]

I want to extract dicts where key=abc or key=mnp, like that i can add multiple condition for key

What should i do here?

I tried to modify this solution but for that i have to do multiple next, each for one condition like key=abc, key=mnp

I can't change the list or how it's formatted because it is response of web request

mihir6692
  • 177
  • 1
  • 4
  • 19

5 Answers5

2
#!/usr/bin/python3
d = [
{'value': 'def', 'key': 'abc'},
{'value': 'xyz', 'key': 'mnp'},
{'value': '456', 'key': '123'},
{'value': '234', 'key': '789'}
]
d = list(filter(lambda x: x['key'] in ['abc','mnp'], d))
print(d)

See it in action here

JGFMK
  • 8,425
  • 4
  • 58
  • 92
1

using generator provided in the question, you make this this a two step part, where you pass your matches into a list then iterate over them using the geneartor

matches = ['abc','mbp']
for match in matches:

    print(next((item for item in d if item["key"] == match), None))
    out:
    {'value': 'xyz', 'key': 'mnp'}
    {'value': 'def', 'key': 'abc'}
Umar.H
  • 22,559
  • 7
  • 39
  • 74
0

Unpack the list, you should edit this.

all_keys = set().union(*(d.keys() for d in mylist))
nakE
  • 362
  • 1
  • 13
0

You can do this:

filtered_entries = [entry for entry in list_of_dicts if entry['key'] in ('abc', 'def', 'ghi', 'hello', 'world')]

Or this:

filtered_entries = filter(lambda entry: entry['key'] in ('abc', 'def', 'ghi', 'hello', 'world'), list_of_dicts)

Or more generally:

def extract_kv_pairs(list_of_dicts, key_condition):
    return [kv for kv in list_of_dicts if key_condition(entry['key'])]

filtered_entries = extract_kv_pairs(list_of_dicts, lambda k: k in ('abc', 'def', 'ghi', 'hello', 'world'))

# Or, if you don't like lambdas:
filtered_entries = extract_kv_pairs(list_of_dicts, ('abc', 'def', 'ghi', 'hello', 'world').__contains__)

# You can use more complicated conditions and put them in named functions:
def condition(key):
    return (len(key)%2 == 0 and 'z' not in key)
filtered_entries = extract_kv_pairs(list_of_dicts, condition)
decorator-factory
  • 2,733
  • 13
  • 25
0

You could transform that list into a dictionary and query your data from it like:

 def check_list():
      list_of_dicts = [{'value': 'def', 'key': 'abc'},
                       {'value': 'xyz', 'key': 'mnp'},
                       {'value': '456', 'key': '123'},
                       {'value': '234', 'key': '789'}
                      ]
      new_dict = {d['key']: d['value'] for d in list_of_dicts}
      abc = d['abc'] if 'abc' in new_dict.keys() else None
      mnp = d['mnp'] if 'mnp' in new_dict.keys() else None

      return abc, mnp

Note that you can implement a different logic for querying or just use a for loop to search it.

Daniel Lima
  • 925
  • 1
  • 8
  • 22