Jan-03-2024, 09:59 PM
(This post was last modified: Jan-03-2024, 09:59 PM by deanhystad.)
Depending on how many strings you have to check I would use sets:
def check_list(items, must_have): """Return True if items list has all elements in must_have list.""" items = {x.lower() for x in items} must_have = set(must_have) return items & must_have == must_have print(check_list(["xxXx", "YyYy"], ("xxxx", "yyyy"))) print(check_list(["xxXx", "YyYy", "ZZZZ"], ("xxxx", "yyyy"))) print(check_list(["xxXx", "YyYy"], ("xxxx", "zzzz")))
Output:True
True
False
Or I would use all()def check_list(items, must_have): """Return True if items list has all elements in must_have list.""" items = [x.lower() for x in items] return all(x in items for x in must_have) print(check_list(["xxXx", "YyYy"], ("xxxx", "yyyy"))) print(check_list(["xxXx", "YyYy", "ZZZZ"], ("xxxx", "yyyy"))) print(check_list(["xxXx", "YyYy"], ("xxxx", "zzzz")))Avoid calling functions more often than needed. Instead of calling lower() for each comparison, create a new list that has lowercase versions of MyList strings.