dting
dting

Reputation: 39287

Getting a set of values from a dictionary of sets

What is the preferred method to get the set of values from a dictionary of sets?

I came up with using reduce and itervalues() was wondering if there is a better method.

>>> m_dict = { 'a': set([1,2]),
... 'b': set([1,4,5]),
... 'c': set([2,8,9]) }
>>> print m_dict
{'a': set([1, 2]), 'c': set([8, 9, 2]), 'b': set([1, 4, 5])}
>>> reduce(lambda x,y:x.union(y), m_dict.itervalues())
set([1, 2, 4, 5, 8, 9])
>>>

Thanks

Upvotes: 2

Views: 1024

Answers (1)

Zach Kelling
Zach Kelling

Reputation: 53869

set.union can accept multiple sets so, set.union(*m_dict.values())

Upvotes: 4

Related Questions