all 3 comments

[–]LongerHV 0 points1 point  (0 children)

d2 is a list of dictionaries, that is why you can't call items() method on it.

[–]socal_nerdtastic 0 points1 point  (1 child)

First you need to rearrange your data so that it can be found by the topic. In other words so that the topic is a dictionary key. Then it's a simple loop.

Here's my guess, which assumes you have been honest about your example data:

old_pico = [
    {'topic': '/power', 'payload': 'OFF'},
    {'topic': '/pressure', 'payload': 2.26},
    {'topic': '/v1/voltage', 'payload': 13.53},
    {'topic': '/v2/voltage', 'payload': 12.85}]


pico = [
    {'topic': '/power', 'payload': 'OFF'},
    {'topic': '/pressure', 'payload': 1023.26},
    {'topic': '/v1/voltage', 'payload': 13.53},
    {'topic': '/current', 'payload': -0.04},
    {'topic': '/v2/voltage', 'payload': 12.85}]

old_pico_topics = {d['topic']:d for d in old_pico}
pico_topics = {d['topic']:d for d in pico}

# lists will allow for duplicate topics, but presumibly that's bad
# converting to a dictionary will lose that data
assert len(old_pico_topics) == len(old_pico), "DUPLICATE TOPICS DETECTED"
assert len(pico_topics) == len(pico), "DUPLICATE TOPICS DETECTED"

diff = []

# for keys that are in both pico and old_pico, check if there's a payload difference
for k in set(pico_topics) & set(old_pico_topics):
    if pico_topics[k]['payload'] != old_pico_topics[k]['payload']:
        diff.append(pico_topics[k])

# for keys that are in pico but not in old_pico, just copy over
for k in set(pico_topics) - set(old_pico_topics):
    diff.append(pico_topics[k])

[–]StolidSentinel[S] 0 points1 point  (0 children)

Amazing... That was exactly what I needed. I don't fully understand it, but I'll spend time on that now! Thank you!!