Dictionaries inside out

Gaëtan Podevijn gpodevij at gmail.com
Fri Nov 26 19:49:48 CET 2010


Found in Dive in Python 3 :

>>> a_dict = {'a': 1, 'b': 2, 'c': 3}
>>> {value:key for key, value in a_dict.items()}
{1: 'a', 2: 'b', 3: 'c'}


2010/11/26 Burton Samograd <burton at userful.com>

> Greg Lindstrom <gslindstrom at gmail.com> writes:
>
> > I am working on a project where I'm using dictionaries to hold the
> > translations to codes (i.e., {'1':'Cheddar','2':'Ice
> > Hockey','IL':'Thermostat Broken'}).  The bulk of the application
> > requires me to translate codes to their meaning, but it would be nice
> > to be able to translate a meaning back to the original code as well.
> > This seems to me like it must be a common situation that has been
> > addresses/solved by those smarter than me.  Is there, dare I say, a
> > design pattern for this problem?  Is there a better way of approaching
> > it other than making a set of dictionaries which "mirror" the
> > originals?  FWIW, I have approximately 50 tables ranging from 2
> > entries to over 100.
>
> Looks like there might be some solutions here:
>
> http://stackoverflow.com/questions/483666/python-reverse-inverse-a-mapping
>
> --
> Burton Samograd
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101126/808a3858/attachment.html>


More information about the Python-list mailing list