Finding values that exist for every dictionary in a list
<p>I have a list of lists that each have several dictionaries. The first list represents every coordinate involved in a triangulation. The second list represents a list of dictionaries associated with that grid coordinate. Each dictionary within the list represents every possible coordinate that is a certain Manhattan distance away from a line point that we are trying to triangulate, our target. Any coordinate that exists within every triangulation list represent the overlapping points from these positions and thus the potential location of our target.</p>

<p>Anyway, I gave some background to hopefully add some understanding. I know it probably comes off confusing, but I'll include my data structure to assist in visualizing what is going on. I've tried looping through these and haven't quite found a way to do this efficiently yet. </p>

<p>Does anyone know of some Python magic to generate a list of the coordinates that exist in every group of coordinates? </p>

<p>So in the example below we are starting with 3 different groups of coordinates. I need to generate a list of any x,y pairs that exists in all 3 groups of coordinates.</p>

<p>Example:</p>

<pre><code>[
    [
        {'x': 1, 'y': 0}, 
        {'x': -1, 'y': 0}, 
        {'x': 0, 'y': 1}, 
        {'x': 0, 'y': -1}
    ],
    [
        {'x': 2, 'y': 0},
        {'x': -2, 'y': 0},
        {'x': 0, 'y': 2},
        {'x': 0, 'y': -2},
        {'x': 1, 'y': 1},
        {'x': -1, 'y': -1},
        {'x': -1, 'y': 1},
        {'x': 1, 'y': -1}
    ],
    [
        {'x': 3, 'y': 0},
        {'x': -3, 'y': 0},
        {'x': 0, 'y': 3},
        {'x': 0, 'y': -3},
        {'x': 2, 'y': 1},
        {'x': -2, 'y': -1},
        {'x': -1, 'y': 2},
        {'x': 1, 'y': -2},
        {'x': 1, 'y': 2},
        {'x': -1, 'y': -2},
        {'x': -2, 'y': 1},
        {'x': 2, 'y': -1}
    ]
]
</code></pre>

<p>There is no magic. You just need to be a bit more careful with your data structures. You are putting coordinates in a dict which are not hashable. So you cannot add them to a set. You need to use tuples. So your data structure should look like this:</p>

<pre><code>my_list = [
    set([
        (1, 0),
        (-1, 0),
        (0, 1),
        (0, -1)
    ]),
    set([
        (1, 0),
        (-2, 0),
        (0, 2),
        (0, -2),
        (1, 1),
        (-1, -1),
        (-1, 1),
        (1, -1)
    ]),
    set([
        (1, 0),
        (-3, 0),
        (0, 3),
        (0, -3),
        (2, 1),
        (-2, -1),
        (-1, 2),
        (1, -2),
        (1, 2),
        (-1, -2),
        (-2, 1),
        (2, -1)
    ])
]
common = my_list[0]
for s2 in my_list[1:]:
    common = common &amp; s2

print common
</code></pre>

python