Skip to content

Instantly share code, notes, and snippets.

@oxtopus
Created April 24, 2012 05:10
Show Gist options
  • Save oxtopus/2476625 to your computer and use it in GitHub Desktop.
Save oxtopus/2476625 to your computer and use it in GitHub Desktop.
dictify/tuplify
>>> dictify = lambda t:dict(zip(*[iter(t)]*2))
>>> dictify(('a', 'A', 'c', 'C', 'b', 'B'))
{'a': 'A', 'c': 'C', 'b': 'B'}
>>>
>>> tuplify = lambda d:tuple(chain(*d.iteritems()))
>>> tuplify({'a': 'A', 'c': 'C', 'b': 'B'})
('a', 'A', 'c', 'C', 'b', 'B')
>>>
>>> assert (lambda d:dictify(tuplify(d))==d)({'a': 'A', 'c': 'C', 'b': 'B'})
>>> assert (lambda t:tuplify(dictify(t))==t)(('a', 'A', 'c', 'C', 'b', 'B'))
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment