Filter list of dicts by highest value of dict and taking reversed values into account
<p>Let's say i have data looking like this:</p>

<pre><code>filter_data = [
    {'sender_id': 1, 'receiver_id': 2, 'order': 1},
    {'sender_id': 2, 'receiver_id': 1, 'order': 3},
    {'sender_id': 3, 'receiver_id': 2, 'order': 5},
    {'sender_id': 2, 'receiver_id': 3, 'order': 2},
]

# there must be a better way to get max elements by reversed keys
# in list of dicts, but I think this whole another question
# so for now let this be this way. 
def get_data():
    qs_data = []
    for data in filter_data:
        for cmp_data in filter_data:
            if data['sender_id'] == cmp_data['receiver_id'] and\
                    data['receiver_id'] == cmp_data['sender_id']:
                if data['order'] &gt; cmp_data['order']:
                    d = data
                else:
                    d = cmp_data
                if d not in qs_data:
                    qs_data.append(d)
    return qs_data
</code></pre>

<p>and desired output will be</p>

<pre><code>[{'order': 3, 'receiver_id': 1, 'sender_id': 2},
 {'order': 5, 'receiver_id': 2, 'sender_id': 3}]
</code></pre>

<p>What my code does it filters <code>filter_data</code> so I will get list of items with highest value of <code>order</code> for <code>sender_id</code> and <code>receiver_id</code> but for me <code>receiver_id=1, sender_id=2</code> is same as <code>sender_id=1, receiver_id=2</code></p>

<p>So my question is is there more pythonic/faster way to do this? Or may be can someone point to direction of improvement.</p>

<p><strong>P.S.</strong> I would much appreciate if someone can come up with understandable title. Sorry for my bad English.</p>

<p>Create an empty dictionary that will gather the new highest dictionary. We iterate through your <code>filter_data</code> and check the sum of <code>sender_id</code> and <code>receiver_id</code>, since you said that the order of those was irrelevant.</p>

<pre><code>filter_data = [
    {'sender_id': 1, 'receiver_id': 2, 'order': 1},
    {'sender_id': 2, 'receiver_id': 1, 'order': 3},
    {'sender_id': 3, 'receiver_id': 2, 'order': 5},
    {'sender_id': 2, 'receiver_id': 3, 'order': 2},
]

new = {}
for d in filter_data:
    total = d['sender_id'] + d['receiver_id']
    if total in new:
        if d['order'] &gt; new[total]['order']:
            new[total] = d
    else:
        new[total] = d

print new.values()
</code></pre>

<p>For example, it will go through the first dictionary and evaluate the sum of its <code>receiver_id</code> and <code>sender_id</code> (The sum is 3). Since we have not encountered a dictionary that has <code>sender_id</code> and <code>receiver_id</code> adding up to 3 yet, it is added to our new dictionary. </p>

<p>However, the next dictionary also has a sum of 3. We check to see if its <code>order</code> value is greater than the previous dictionary. Since it is, it overrides that former dictionary.</p>

<p>Then we print the values of our new dictionary since the keys only contain the sum of <code>sender_id</code> and <code>receiver_id</code>.</p>

python