Search code examples
pythonpython-3.xunique

Get unique values from second of two 2 dimensinal lists


I asked about finding non-duplicated items from two lists in my old post and received the answer.

I have now realised I have a different requirement but can't adapt my code or the answers I got there.

I have two list which is for example

List1= [[1,2],[3,5],[4,4],[5,7]]
List2= [[1,3],[4,4],[3,5],[3,5],[5,6]]
Result= [[1,3],[5,6]]

I want the unique values from second list only (not both)

My script:

def filter2dim(firstarray, secondarray):
    unique = []
    for i in range(len(firstarray)):
       temp=firstarray[i]
       for j in range(len(secondarray)):
           if(temp == secondarray[j]):
              break
           elif(j==(len(secondarray)-1)):
               unique.append(temp)
    for i in range(len(secondarray)):
       temp=secondarray[i]
       for j in range(len(firstarray)):
           if(temp == firstarray[j]):
              break
           elif(j==(len(firstarray)-1)):
               unique.append(secondarray[i])
    return unique

This doesn't quite do what I want - can anyone suggest what I could do to make it return only values unique to the second list?


Solution

  • Okay, Sorry if i made an issue, but i found an answer, after implementing

        a = {(1,2),(3,5),(4,4),(5,7)}
        b = {(1,3),(4,4),(3,5),(3,5),(5,6)}
        print(a.symmetric_difference(b)) # {(1, 2), (1, 3), (5, 6), (5, 7)}
    


    Suggested in the first question i asked, i went to google and searched what does symmetric_difference does and if there is any alternatives, and found the answer in the following https://docs.python.org/2/library/sets.html which is a.difference(b)

        a = {(1,2),(3,5),(4,4),(5,7)}
        b = {(1,3),(4,4),(3,5),(3,5),(5,6)}
        print(a.difference(b)) # {(1, 2),(5, 7)}
    
        a = {(1,2),(3,5),(4,4),(5,7)}
        b = {(1,3),(4,4),(3,5),(3,5),(5,6)}
        print(b.difference(a)) # {(1,3),(5,6)}
    

    thank you, best regards.