How to Identify the elements which are removed from set() in python? How to Identify the elements which are removed from set() in python? python-3.x python-3.x

How to Identify the elements which are removed from set() in python?


You don't even need set. You want a count of each element with more than one occurrence. Counter from collections together with a dictionary comprehension should get you there.

from collections import Countera = [1, 1, 1, 2, 2, 3, 4]    removed = {k: v-1 for k, v in Counter(a).iteritems() if v > 1}>>> removedOut[8]: {1: 2, 2: 1}


collections.Counter is useful here.

from collections import Countercounts = Counter(a)b = set(counts.keys())for x, count in counts.items():    if count > 1:        print('%d appearances of %s were removed in the set' % (count-1, x))


Try this with Counter

from collections import Countera = [1, 2, 3, 1, 4]>>>[i for i in Counter(a) if Counter(a)[i] > 1][1]