find repeated element in list of list python
<p>I have been struggling with this problem for two days and I need help with it. I need to find repeated element in a list of lists
<code>list_of_list = [(a1, b1, c1), (a2, b2, c2), ..., (an, bn, cn)]</code> where "a" and "b" elements are integers and "c" elements are floats.</p>

<p>So if for example <code>a1 == a2</code> or <code>a1 == bn</code>, I need to create a new list with the entire list elements and I need to iterate this for all the lists (a, b, c) in the list of lists. To put it another way, I need all lists that have elements that are present in more than one list. I need to compare only "a" and "b" elements but obtain the associated value "c" in the final list.</p>

<p>For example:</p>

<pre><code>list_of_list = [(1, 2, 4.99), (3, 6, 5.99), (1, 4, 3.00), (5, 1, 1.12), (7, 8, 1.99) ]

desired_result=[(1, 2, 4.99), (1, 4, 3.00), (5, 1, 1.12)] 
</code></pre>

<p>I try many ideas...but nothing nice came up:</p>

<pre><code>MI_network = [] #repeated elements list
genesis = list(complete_net) #clon to work on
genesis_next = list(genesis) #clon to remove elements in iterations
genesis_next.remove(genesis_next[0])

while genesis_next != []:
    for x in genesis:
        if x[0] in genesis_next and x[1] not in genesis_next:
            MI_network.append(x)
        if x[0] not in genesis_next and x[1] in genesis_next:
            MI_network.append(x)
    genesis_next.remove(genesis_next[0])
</code></pre>

<p>And this is how i would do it since i was not aware of the <code>collections.defaultdict()</code>.</p>

<pre><code>list_of_list = [(1, 2, 4.99), (3, 6, 5.99), (1, 4, 3.00), (5, 1, 1.12), (7, 8, 1.99) ]
results = []
for i_sub, subset in enumerate(list_of_list):
# test if ai == aj
    rest = list_of_list[:i_sub] + list_of_list[i_sub + 1:]
    if any(subset[0] == subrest[0] for subrest in rest):
        results.append(subset)
# test if ai == bj
    elif any(subset[0] == subrest[1] for subrest in rest):
        results.append(subset)
# test if bi == aj
    elif any(subset[1] == subrest[0] for subrest in rest):
        results.append(subset)
print(results)  # -&gt; [(1, 2, 4.99), (1, 4, 3.0), (5, 1, 1.12)]
</code></pre>

python list element