#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
#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']} |