I found myself needed zip but for dictionaries, and wanted to know if there is any way I can make this shorter and more concise, and/or faster. Also I cannot figure out what the type hinting might be for the return (I would like to put it in the reStructuredText).
def dict_zip(*args):
"""The zip utility but for dictionaries
:param args:
A bunch of **identical** dicts
:type args:
dict
:return:
A dict of all the keys in the input dict,
mapped to a list of all the values fro that key
:rtype:
dict
"""
out = {i: [] for i in args[0]}
for dic in args:
for key in out:
out[key].append(dic[key])
return out
if __name__ == "__main__":
dict1 = {"first": True, "name": "John"}
dict2 = {"first": False, "name": "sue"}
print dict_zip(dict1, dict2)
Output:
{'name': ['John', 'sue'], 'first': [True, False]}