22

Ok, I am trying to filter a list/dictionary passed to me and "clean" it up a bit, as there are certain values in it that I need to get rid of.

So, if it's looking like this:

"records": [{"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4": "AAA"...}]

How would I quickly and easily run through it all and replace all values of "AAA" with something like "XXX"?

Focus is on speed and resources, as these may be long lists and I don't want this process to consume too much time.

3 Answers 3

29
DATA = {"records": [{"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4": "AAA"}]}

for name, datalist in DATA.iteritems():  # Or items() in Python 3.x
    for datadict in datalist:
        for key, value in datadict.items():
            if value == "AAA":
                datadict[key] = "XXX"

print (DATA)
# Prints {'records': [{'key3': 'CCC', 'key2': 'BBB', 'key1': 'XXX', 'key4': 'XXX'}]}
1
  • Yep, that worked like a charm. I was missing one of the outer loops and getting list/items() error... thank you!
    – Crazy Serb
    Commented Jul 2, 2009 at 20:55
11
dic = root['records'][0]
for i, j in dic.items():       # use iteritems in py2k
    if j == 'AAA':
        dic[i] = 'xxx'
0

As far as I concerned, using if/else in a dictionary comprehension is much faster than the above answers. In the following, I have provided a generic example for your use case:

DATA = {"records_0": [{"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4": "AAA"},
                      {"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4": "AAA"}],
        "records_1": [{"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4": "AAA"}]}

Replaced_DATA = {k:[
{k_0:v_0 if v_0!='AAA' else 'XXX' for k_0,v_0 in v_.items()} for v_ in v] for k,v in DATA.items()}

Here is the output:

Replaced_DATA
Out[1]. {'records_0': [{'key1': 'XXX', 'key2': 'BBB', 'key3': 'CCC', 'key4': 'XXX'}, 
                       {'key1': 'XXX', 'key2': 'BBB', 'key3': 'CCC', 'key4': 'XXX'}],
         'records_1': [{'key1': 'XXX', 'key2': 'BBB', 'key3': 'CCC', 'key4': 'XXX'}]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.