[Tutor] A program that can check if all elements of the list are mutually disjoint

Manprit Singh manpritsinghece at gmail.com
Sat Jun 5 09:09:19 EDT 2021


Dear sir,
Consider a list :
ls = ["amba", "Joy", "Preet"]

The elements of the list are mutually disjoint, as no two elements of the
list have anything in common.

I have written the code but it seems quite unreadable.  First of all need
to know if it is correct or not, if it is correct , I need to know if this
can be done in a better way or not. Kindly have a look at below given code :

ls = ["amba", "Joy", "Preet"]
for idx, ele in enumerate(ls):
    if idx < len(ls)-1:
        if not all(set(ele).isdisjoint(ch)for ch in ls[idx+1:]):
            print("Not mutually disjoint")
            break
else:
    print("Mutually disjoint")

Regards
Manprit Singh


More information about the Tutor mailing list