print item(s) that do not exist in both list

107 Views Asked by At
row_names=['a','b','b','b']
col_names=['a','a','a','b','b','b']

How do I only print items that do not match in both? Zip doesnt work due to unequal lengths.

Something along this line?

 for item in row_names, col_names:
    if row_names[item] != col_names[item]:
        print item
2

There are 2 best solutions below

0
On BEST ANSWER

Use set.symmetric_difference:

results = set(col_names).symmetric_difference(set(row_names))
# Or
results = set(col_names) ^ set(row_names)
0
On
row_names=['a','b','b','b']
col_names=['a','a','a','b','b','b']

def diff(row,col):
    big, little = [row,col] if len(row) > len(col) else [col,row]
    map (lambda item: big.pop(big.index(item)) if item in big else None,little) 
    return big

print 'res: {}'.format(diff(row_names,col_names))

res: ['a', 'a']