Removing duplicate content from a list of lists while not preserving any order

  • A+
Category:Languages

I was writing a code in python to find factor pairs for an integer. But making pairs resulted in reverse pairs as well. I want to eliminate those reverse pairs using a simple method without importing any modules. for eg.

[[1, 200], [2, 100], [4, 50], [5, 40], [8, 25], [10, 20], [20, 10], [25, 8], [40, 5], [50, 4], [100, 2], [200, 1]]

the output should be:

[[1, 200], [2, 100], [4, 50], [5, 40], [8, 25], [10, 20]] 

This is what I've got so far:

N = [] J = [] F = [] Z = [] S = [] num = input("Enter no. of elements in list") print ('Enter numbers') prod = 1 for i in range(int(num)):     n = input("num :")     N.append(int(n)) for x in N:     prod = prod*x print (prod) k = input("Enter no. of splits:") for o in range(1,prod+1):     if prod%o == 0:         J.append(o)         F.append(o) print (J)  Z = [[a, b] for a in J for b in F if a*b == prod] print (Z) 

 


>>> l = [[1, 200], [2, 100], [4, 50], [5, 40], [8, 25], [10, 20], [20, 10], [25, 8], [40, 5], [50, 4], [100, 2], [200, 1]] >>> new_l = [] >>> for e in l: ...     if e not in new_l and sorted(e) not in new_l: ...         new_l.append(e) ...  >>> new_l [[1, 200], [2, 100], [4, 50], [5, 40], [8, 25], [10, 20]] >>>  

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: