“Invert” a dict in python

If you have a list/tuple in python and you want to use the items in there to return an index of the position of the item, you can do so like this:


a = ('archive', 'audio', 'document', 'video', 'executable',
'image', 'misc', 'parity')
lookup = { v:k for k,v in enumerate(a) }

Leave a Reply

Your email address will not be published. Required fields are marked *