python - Removing duplicates from two lists -
python - Removing duplicates from two lists -
i have written code in python remove duplicate items lists. have 2 separate lists like:
lhsnet = ['p', 'p', 'p', 'p', '(2)h', 'p', '(2)h', 'p', '(3)he', '(3)he'] rhsnet = ['(2)h', 'e+', 'nu_e', '(2)h', 'e+', 'nu_e', '(3)he', 'gamma', '(3)he', 'gamma', '(4)he', 'p', 'p']
here code:
for x in lhsnet: z in rhsnet: if x == z: lhsnet.remove(x) rhsnet.remove(z) break
the code should find duplicate entry exists in both lists , remove it. reason after execution left with:
lhsnet = ['p', 'p', 'p', 'p', '(3)he'] rhsnet = ['e+', 'nu_e', 'e+', 'nu_e', 'gamma', '(3)he', 'gamma', '(4)he']
clearly has removed of duplicate entries exist in both lists except lastly '(3)he'
. can explain me going wrong in code , how prepare it?
you shouldn't utilize break statement when iterate if remove , create code like.
x in lhsnet: z in rhsnet: if x == z: lhsnet.remove(x) rhsnet.remove(z)
everything working perfectly
python list duplicates
Comments
Post a Comment