Created
January 31, 2022 08:09
-
-
Save avalanchy/3e8fa3a4e157eec3e27405066b99707d to your computer and use it in GitHub Desktop.
python function that returns cartesian prodict of two lists containing only pairs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def product_of_pairs(iterable1, iterable2, item_sorting_key=None): | |
"""Function that returns cartesian product of two lists. Output is filtered to contain only pairs, where pair | |
means item cannot be with itself and AB == BA. | |
Example: | |
>>> product_of_pairs('ab', 'ab') | |
{('a', 'b')} | |
""" | |
return { | |
tuple(sorted((item1, item2), key=item_sorting_key)) | |
for item1 in iterable1 | |
for item2 in iterable2 | |
if item1 != item2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment