pythonlist

Count how often an element in a list of dicts is unequal to "X"


I have list of dicts. Each dict has the same items with a different value. It is a question code with the answer. So question "a" can be answerd with a scale of 1-7. If you do not want to answer that question, it is an "X" in the answer sheet.

example:

[
{"a":1,"b":2,"c":"X"},
{"a":1,"b":"X","c":3},
{"a":1,"b":2,"c":"X"}
]

My goal is to know, if there is any question, that has less than 5 or more than 50 valid answers. A valid answer is anything except "X". So I need to count now, how often any item is not equal "X". In that case, for "a" it would be 3, for "b" it would be 2 and for "c" it would be 1.

I could loop through it. Make an array for each item in the dicts. Then loop trough the list, inside that loop through the dict and add +1 to the item in the array. Afterwards check the counter, if there is any item < 5 or > 50.

But there is probably some sweet python code (as always) that does this in like 3 lines. While my loop would probably be 5 times bigger. And is faster. I have to repeat that check a few thousand times. So speed is kinda important. Not super duper important, but nice to have


Solution

  • As everything is loaded in memory, I would just iterate the keys, counting the number of 'X' per key. Assuming your list of dictionary is ld I would try:

    keys = ld[0].keys()    # ignore this if you already have a list of the keys
    missing = {k: sum(d[k] == 'X' for d in ld) for k in keys}
    

    With your sample data, it returns - as expected:

    {'a': 0, 'b': 1, 'c': 2}
    

    Of course this only makes sense if no dictionary after the first one contains a new key...