Set of Dictionary

Konstantin Veretennicov kveretennicov at gmail.com
Thu Jun 16 11:02:00 EDT 2005


On 6/16/05, Vibha Tripathi <vibtrip at yahoo.com> wrote:
> Hi Folks,
> 
> I know sets have been implemented using dictionary but
> I absolutely need to have a set of dictionaries...

While you can't have a set of mutable objects (even dictionaries :)),
you can have a set of immutable snapshots of those objects:

>>> d1 = {1: 'a', 2: 'b'}
>>> d2 = {3: 'c'}
>>> s = set([tuple(d1.iteritems()), tuple(d2.iteritems())])
>>> s
set([((3, 'c'),), ((1, 'a'), (2, 'b'))])
>>> [dict(pairs) for pairs in s]
[{3: 'c'}, {1: 'a', 2: 'b'}]

- kv



More information about the Python-list mailing list