Let's try this solution which covers all the scenarios, which is scalable and preserves the order of all items in each list:
from itertools import chain, islice, zip_longest
def partners(l1, l2):
length = min(len(l1), len(l2)) # Number of common items
big_list = chain(*zip(l1, l2), islice(l1, length, None), islice(l2, length, None))
for who, partner in zip_longest(big_list, big_list):
print(f'{who} and {partner}' if partner else f'{who} is alone')
List1 = ['Drew', 'Ken']
List2 = ['Ralph', 'Kevin']
print("Matched Pairs")
partners(List1, List2)
List1 = ['Drew', 'Ken', 'Mat']
List2 = ['Ralph', 'Kevin']
print("\nUnmatched Pairs")
partners(List1, List2)
List1 = ['Drew', 'Ken', 'Mat', 'Jay']
List2 = ['Ralph', 'Kevin']
print("\nSuperfluous Pairs")
partners(List1, List2)
List1 = ['Drew']
List2 = []
print("\nSingle Player")
partners(List1, List2)
List1 = []
List2 = []
print("\nNobody")
partners(List1, List2)
Matched Pairs
Drew and Ralph
Ken and Kevin
Unmatched Pairs
Drew and Ralph
Ken and Kevin
Mat is alone
Superfluous Pairs
Drew and Ralph
Ken and Kevin
Mat and Jay
Single Player
Drew is alone
Nobody
The way this works is big_list
consists of the following:
- Zip (Pair) up one from each list and flatten that list (
*zip(l1, l2)
)
- The list of items in
l1
not in l2
- The list of items in
l2
not in l1
This results in big_list having the universal order of all partners as [a1, a2, b1, b2, c1, c2, ...] where same letter = partner.
Then we used the grouper()
recipe and partner everyone up and loop through it with any unpaired partnered with "No one"
.