Skip to content

Instantly share code, notes, and snippets.

@avalanchy
Created January 31, 2022 08:09
Show Gist options
  • Save avalanchy/3e8fa3a4e157eec3e27405066b99707d to your computer and use it in GitHub Desktop.
Save avalanchy/3e8fa3a4e157eec3e27405066b99707d to your computer and use it in GitHub Desktop.
python function that returns cartesian prodict of two lists containing only pairs
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