Python - compare nested lists and append matches to new list?

  • Use sets to obtain collections with no duplicates.

    • You'll have to use tuples instead of lists as the items because set items must be hashable.
  • The code you posted doesn't seem to generate the output you posted. I do not have any idea how you are supposed to generate that output from that input. For example, the output has 'y' and the input does not.

  • I think the design of your function could be much improved. Currently you define x, y, and match as the module level and read and mutate them explicitly. This is not how you want to design functions—as a general rule, a function shouldn't mutate something at the global level. It should be explicitly passed everything it needs and return a result, not implicitly receive information and change something outside itself.

    I would change

    x = some list
    y = some list
    match = []
    def find_match():
        for i in x:
            for j in y:
                if i[0] == j[0]:
                     match.append(j)
        return match # This is the only line I changed. I think you meant 
                     # your return to be over here?
    find_match()
    

    to

    x = some list
    y = some list
    
    def find_match(x, y):
        match = []
        for i in x:
            for j in y:
                if i[0] == j[0]:
                     match.append(j)
         return match
    match = find_match(x, y)
    
  • To take that last change to the next level, I usually replace the pattern

    def f(...):
        return_value = []
        for...
            return_value.append(foo)
        return return_value
    

    with the similar generator

    def f(...):
        for...
            yield foo
    

    which would make the above function

    def find_match(x, y):
        for i in x:
            for j in y:
                if i[0] == j[0]:
                     yield j
    

    another way to express this generator's effect is with the generator expression (j for i in x for j in y if i[0] == j[0]).


I don't know if I interpret your question correctly, but given your example it seems that you might be using a wrong index:

change

if i[1] == j[1]:

into

if i[0] == j[0]:

You can do this a lot more simply by using sets.

set_x = set([i[0] for i in x])
set_y = set([i[0] for i in y])
matches = list(set_x & set_y)