Skip to main content
2 of 6
added info about `typing.MutableMapping`
kyrill
  • 1.6k
  • 11
  • 24

A shorter version, functionally equivalent to yours:

def dict_zip(*dicts):
    return {k: [d[k] for d in dicts] for k in args[0].keys()}

That's assuming all dicts have the same keys, or more exactly, all dicts have at least all the keys present in the first dict.

To make it more robust and handle cases when dicts don't have the same keys:

def dict_zip(*args):
  all_keys = {k for a in args for k in a.keys()}
  return {k: [a[k] for a in args if k in a] for k in all_keys}

Regarding type hints: not sure, but it might be something based on typing.MutableMapping. But that's Python 3.

kyrill
  • 1.6k
  • 11
  • 24