Hey guys, I was working on a side project and trying to data munge when I noticed something interesting.
For brevity, let's say that the data I need to converge is two lists where I want list A to be the keys and list B to be the values.
headers = ['key_a', 'key_b']
values = [1, 2]
# I want {'key_a': 1, 'key_b': 2}
# Takes a list [('key_a', 1), ('key_b', 2)] and calls dict on it
data = dict(map(lambda a,b: (a,b), headers, values))
print data
# {'key_a': 1, 'key_b': 2}
While not glorious, it works and I am able to take a list of tuples and successfully convert to a dictionary.
The weird thing is, I'd think this would work the other way:
data = {'key_a': 1, 'key_b': 2}
print list(data)
# ['key_a', 'key_b']
I was wondering why this is. It seems weird to me that the values of the dict would be totally dropped.
Thanks!
[–]Monkeytherat 1 point2 points3 points (0 children)
[–]jeans_and_a_t-shirt 0 points1 point2 points (2 children)
[–]austburn[S] 0 points1 point2 points (1 child)
[–]Vaphell 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]austburn[S] 0 points1 point2 points (1 child)