Python Frozenset

2.4k views Asked by At

I am using frozenset and I would like to avoid the output containing 'frozenset'. For example, I have

x = [frozenset([item]) for item in Set]

Output: frozenset(['yes']) => frozenset(['red', 'blue'])

Any ideas?

1

There are 1 answers

2
Ashwini Chaudhary On BEST ANSWER

You can do this by creating a subclass of frozenset and overriding its __repr__ method:

class MyFrozenSet(frozenset):
    def __repr__(self):
        return '([{}])'.format(', '.join(map(repr, self)))
...     
>>> lst = [['yes'], ['red', 'blue']]
>>> [MyFrozenSet(x) for x in lst]
[(['yes']), (['blue', 'red'])]