Skip to content

Instantly share code, notes, and snippets.

@benjaminmgross
Last active August 29, 2015 14:02
Show Gist options
  • Save benjaminmgross/e5c368b725987eab8591 to your computer and use it in GitHub Desktop.
Save benjaminmgross/e5c368b725987eab8591 to your computer and use it in GitHub Desktop.
Makin' a Shexy `dict`

#Motivation

I was getting fed up with the usual, list of keys, looping through, and assigning a value to the key... I wanted something a little shexxier using Python's map functionality.

Here's what I came up with

##Makin' a Sexy Dict

In [57]: keys = ['a', 'b', 'c']
In [58]: vals = [[1, 2, 3], [3, 2, 1], ['yes', 'no', 'maybe']]
#Normal method
In [59]: bland_dict = {}
In [60]: for i, key in enumerate(keys):
...: bland_dict[key] = vals[i]
#result
In [61]: bland_dict
Out[61]: {'a': [1, 2, 3], 'b': [3, 2, 1], 'c': ['yes', 'no', 'maybe']}
#Shexshy dict
In [62]: shexy_dict = dict(map(lambda x, y: [x, y], keys, map(lambda x: x, vals)))
In [64]: shexy_dict
Out[64]: {'a': [1, 2, 3], 'b': [3, 2, 1], 'c': ['yes', 'no', 'maybe']}

#Conclusion

I know what you're thinking...

Shexxshy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment