Python – Disjoint Strings across Lists

PythonServer Side ProgrammingProgramming

When it is required to find disjoint strings across lists, a method is defined that takes two parameters, and uses the lambda and reduce methods with the ‘if’ condition to determine the result.

Below is a demonstration of the same −


 Live Demo

from functools import reduce

def determine_disjoint_pairs(disjoint_data, my_result=[]):

   if not disjoint_data and not reduce(lambda a, b: set(a) & set(b), my_result):
      yield tuple(my_result)
   elif disjoint_data:
      yield [idx for k in disjoint_data[0] for idx in determine_disjoint_pairs(disjoint_data[1:], my_result + [k])]

my_list_1 = ["python", "is", "fun"]
my_list_2 = ["its", "awesome", "learning"]

print("The first list is : ")
print("The second list is :")

my_result = list(determine_disjoint_pairs([my_list_1, my_list_2]))

print("The result is :")


The first list is :
['python', 'is', 'fun']
The second list is :
['its', 'awesome', 'learning']
The result is :
[('fun', 'its'), ('fun', 'awesome')]


  • A method named ‘determine_disjoint_pairs’ is defined that takes two parameters.

  • If the first parameter is not true, and the ‘reduce’ and ‘lambda’ methods used with two parameters is not true, the yield operator is used to determine the result, which is also converted to a tuple.

  • Otherwise, yield operator along with list comprehension is used by calling the method again with a different set of parameters.

  • Outside the method, two lists of strings are defined and displayed on the console.

  • The method is called by passing these two lists.

  • It is converted to a list and is assigned to a variable.

  • This variable is displayed as output on the console.

Published on 06-Sep-2021 07:50:57