-1

I'm new to Python and I'm still learning it. I need some help on how to pair items from two lists and also how to implement custom pairing if one list has more items than the other list.

Example if both lists are the same length:

List1 = [Drew, Ken]
List2 = [Ralph, Kevin]

Output should be like this:

Drew and Ralph
Ken and Kevin

Another example if one list has one extra item:

List1 = [Drew, Ken, Mat]
List2 = [Ralph, Kevin]

Output should be like this:

Drew and Ralph
Ken and Kevin
Mat is alone

Last example if the one list has two extra items:

List1 = [Drew, Ken, Mat, Jay]
List2 = [Ralph, Kevin]

Output should be like this:

Drew and Ralph
Ken and Kevin
Mat and Jay
jarmod
  • 71,565
  • 16
  • 115
  • 122
Faded
  • 31
  • 1
  • 9

4 Answers4

2

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:

  1. Zip (Pair) up one from each list and flatten that list (*zip(l1, l2))
  2. The list of items in l1 not in l2
  3. 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".

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
  • its working. but when i try making list1 with 5 index and list2 with 3 index there is an error. There is "is with" and "no one" together in the last line when printing it – Faded Dec 27 '19 at 19:26
  • @Faded, are you sure? I tested with `List1, List2 = ['Drew', 'Ken', 'Mat', 'Jay', 'Faded'], ['Ralph', 'Kevin', 'LaughDonor']`, which is where `len(List1) == 5` and `len(List2) == 3` as you explain in your comment. And this works without error. – Sunny Patel Dec 27 '19 at 19:28
  • This solution utilizes some advanced functions in Python which may not be suitable for homework (as you have explained in recent previous questions asked). I don't suggest turning this in unless you completely understand what is happening here. – Sunny Patel Dec 27 '19 at 19:30
  • @Faded, Still works. You may have a typo in your implementation. – Sunny Patel Dec 27 '19 at 19:34
  • sorry for typo again. its with 5 index and 4 index. thats the final – Faded Dec 27 '19 at 19:35
  • oh its working in your code. but when i try it in my code, its not working. my bad sir, im sorry. – Faded Dec 27 '19 at 19:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204964/discussion-between-sunny-patel-and-faded). – Sunny Patel Dec 27 '19 at 19:53
1

Match the list sizes and zip them.

>>> l1 = ['Pedro', 'Rodrigues', 'Faded']
>>> l2 = ['Someone']
>>> for _ in range(abs(len(l1) - len(l2))):
...     l2.append('No one')
... 
>>> for who, with_who in zip(l1, l2):
...     print(f'{who} is with {with_who}')
... 
Pedro is with Someone
Rodrigues is with No one
Faded is with No one
>>> 
Pedro Rodrigues
  • 2,520
  • 2
  • 27
  • 26
  • it works if other list having only 1 extra index, but how can you pair the last two index of list1 if list1 having 2 more index to list2? – Faded Dec 27 '19 at 17:31
  • @Faded I've updated the example to have a diff of 2 between the list lens, I believe that is the concern you brought forward. The only situation where this fails is if `l1`'s len is actually smaller than `l2`'s len, otherwise `l2` will be appended to match `l1`'s len. Does that address your question? – Pedro Rodrigues Dec 27 '19 at 18:39
  • 1
    Instead of `max(len(l1), len(l2)) - min(len(l1), len(l2))`, could also do `abs(len(l2) - len(l1))`. Also, maybe you could incorporate [`zip_longest`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest)? – Sunny Patel Dec 27 '19 at 18:39
  • OP's last example shows that the Rodrigues and Faded should be paired. – Sunny Patel Dec 27 '19 at 18:42
  • @SunnyPatel it is ambiguous, then, from where should you start merging. Either from the end or the point of difference. I did miss it. But it boils down to the same reduction to a normal zip. – Pedro Rodrigues Dec 27 '19 at 18:43
  • This also mutates the original lists, which is undesirable. – jarmod Dec 27 '19 at 19:28
  • @jarmod well, don't mutate them then, should not be that difficult if you got that far. – Pedro Rodrigues Jan 04 '20 at 23:04
  • 1
    @PedroRodrigues I'm not the OP. I'm making a general comment that if you write a function to calculate a result given some input data, you should not modify that input data. – jarmod Jan 05 '20 at 03:14
  • @jarmod I agree. That is what is known as side effects. They are better avoided. – Pedro Rodrigues Jan 05 '20 at 05:36
1

Using Zip makes up only for the first case you described. But if you just want the job done, then you can use this.

list1 = ["Manish","Kalol","Haha","Ram","lala"]
list2 = ["lol","Hmmmm"]

if len(list1)>=len(list2):
    biggerlist = list1
    smallerlist = list2
else:
    smallerlist = list1
    biggerlist  =list2

for i in range(len(smallerlist)):    
    print(biggerlist[i] + " with "+ smallerlist[i])

for i in range(len(smallerlist),len(biggerlist)-1 ,2):
    print(biggerlist[i] + " with " + biggerlist[i+1])

if(not (len(biggerlist)-len(smallerlist))%2==0):
    print(biggerlist[len(biggerlist)-1] + " is alone")
0

I think this can be a beginning and i can improve it with some time in a simple way.

List1 = ['Drew', 'Ken']
List2 = ['Ralph', 'Kevin']

Listx = list(zip(List1, List2))
for item in Listx:
    print(f'{item[0]} and {item[1]}')
Kevin Omar
  • 127
  • 9