Generic dictionary
Thorsten Kampe
thorsten at thorstenkampe.de
Sun Nov 20 07:19:03 EST 2016
* Anny Mous (Sun, 20 Nov 2016 21:46:25 +1100)
>
> On Sun, 20 Nov 2016 08:43 pm, Peter Otten wrote:
>
> > Thorsten Kampe wrote:
> >
> >> [Crossposted to tutor and general mailing list]
> >>
> >> Hi,
> >>
> >> I'd like to extend the dictionary class by creating a class that acts
> >> like a dictionary if the class is instantiated with a dictionary and
> >> acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
> >> instantiated with a list (that is dictitem).
> [...]
> > def GenericDict(dict_or_items):
> > if isinstance(dict_or_items, dict):
> > return dict(dict_or_items)
> > else:
> > return SimpleGenericDictWithOnlyTheFalseBranchesImplemented(
> > dict_or_items
> > )
>
>
> Personally, I'd go even simpler:
>
> dict(dict_of_items)
>
> will return a dict regardless of whether you start with another dict or a
> list or tuples.
The whole point of my posting was non hashable keys (like lists):
```
>>> dictitem
[([1], '11'), ([2], '22'), ([4], '33'), ([3], '44')]
>>> dict(dictitem)
---------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-0f2b626ac851> in <module>()
----> 1 dict(dictitem)
TypeError: unhashable type: 'list'
```
Thorsten
More information about the Python-list
mailing list