Skip to content

Consider adding a zipdict() function #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ankostis opened this issue Feb 21, 2016 · 5 comments
Open

Consider adding a zipdict() function #291

ankostis opened this issue Feb 21, 2016 · 5 comments

Comments

@ankostis
Copy link

Please consider adding a zipdict() function that mimics the zip() function but on dictionaries This utility allows to iterate the items (key,value) of a list of dictionaries based on their common keys.

The necessity for such a function is described in this SO question: http://stackoverflow.com/questions/16458340/python-equivalent-of-zip-for-dictionaries

A starting implementation would be based on @Volatilty's suggestion in the above question:

def zipdict(*dcts):
    if dcts:
        for k in set(dcts[0]).intersection(*dcts[1:]):
            yield k, tuple(d[k] for d in dcts)

@ankostis ankostis changed the title Consider adding a zipdict() fnction Consider adding a zipdict() function Feb 21, 2016
@eriknw
Copy link
Member

eriknw commented Feb 25, 2016

Interesting suggestion. If you knew the keys in the dict are identical, one could simply do:

>>> import toolz
>>> da = {'a': 1, 'b': 2, 'c': 3}
>>> db = {'a': 4, 'b': 5, 'c': 6}
>>> toolz.merge_with(tuple, da, db)
{'a': (1, 4), 'c': (3, 6), 'b': (2, 5)}

If the keys aren't identical, then the operation seems weird to me. Should you only include the intersection of keys, use a default for missing values, or make this an option? Should this be left for the user to spell out explicitly using keyfilter on the inputs or valfilter on the output?

My inclination would be to have zipdict return a dict if it's added to dicttoolz, not a generator of items. Thoughts?

@ankostis
Copy link
Author

ankostis commented Feb 25, 2016

If the keys aren't identical, then the operation seems weird to me.

Typically the keys partly overlap; but actually it shouldn't matter.
I had crafted some elaborate performance code to check for various contention cases, if you are interested.

Should you only include the intersection of keys, use a default for missing values, or make this an option?

zip() iterates just on the shortest list (so "intersection" it is) - why should zipdict() support otherwise?

My inclination would be to have zipdict return a dict if it's added to dicttoolz, not a generator of items. Thoughts?

I had also given thought on that.
I would follow the zip() contract again, and return a generator - so it should be added, counter-intuitively I musts say, into the itertoolz :-)

@ankostis
Copy link
Author

ankostis commented Sep 8, 2016

Any progress on that?

@ankostis
Copy link
Author

ping??

@groutr
Copy link
Contributor

groutr commented Jun 5, 2018

This is interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants