Last Updated: February 25, 2016
·
290
· hemraj

Python - No Looping for difference and removal of duplicate elements.

Getting difference between tuples

a = ('one',2,'three','five')
b = (2,'five','six')
c = tuple(set(a) - set(b)) # ('three','one')

Getting difference between list

a = ['one',2,'three','five']
b = [2,'five','six']
c = list(set(a) - set(b)) # ['three','one']

Getting difference between dictionary keys

a = {1:'one',2:'two',3:'three',5:'five'}
b = {2:'two',5:'five',6:'six'}
c = list(set(a.keys()) - set(b.keys())) # ['three','one']

Eliminating duplicate elements without looping:

a = [1,2,1,4,5,2]
b = list(set(a))    # [1,2,4,5]