find repeated element in list of list python
Question: 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 `list_of_list = [(a1, b1,
c1), (a2, b2, c2), ..., (an, bn, cn)]` where "a" and "b" elements are integers
and "c" elements are floats.

So if for example `a1 == a2` or `a1 == bn`, 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.

For example:

    
    
    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)] 
    

I try many ideas...but nothing nice came up:

    
    
    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])
    


Answer: You can count occurrences of specific list elements and take lists with counts
> 1. Something like this, using `collections.defaultdict()`:

    
    
    >>> from collections import defaultdict
    >>> count = defaultdict(int)
    >>> for lst in list_of_list:
    ...     count[lst[0]] += 1
    ...     count[lst[1]] += 1
    ...
    >>> [lst for lst in list_of_list if count[lst[0]] > 1 or count[lst[1]] > 1]
    [(1, 2, 4.99), (1, 4, 3.0), (5, 1, 1.12)]
    

