Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] list content check
#4
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.
Reply


Messages In This Thread
[solved] list content check - by paul18fr - Jan-03-2024, 02:10 PM
RE: list content check - by buran - Jan-03-2024, 02:46 PM
RE: list content check - by paul18fr - Jan-03-2024, 04:02 PM
RE: list content check - by deanhystad - Jan-03-2024, 09:59 PM
RE: list content check - by Pedroski55 - Jan-04-2024, 10:01 AM
RE: list content check - by paul18fr - Jan-04-2024, 10:16 AM
RE: [solved] list content check - by deanhystad - Jan-04-2024, 11:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 571 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,713 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,222 Jan-28-2023, 12:53 PM
Last Post: Winfried
  Check if clients are online with ips stored in json [SOLVED] AlphaInc 6 2,664 Jun-27-2022, 08:28 AM
Last Post: AlphaInc
  check if element is in a list in a dictionary value ambrozote 4 2,115 May-11-2022, 06:05 PM
Last Post: deanhystad
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,232 May-11-2022, 02:23 PM
Last Post: menator01
  [solved] Basic question on list matchiing paul18fr 7 1,982 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  How to check if a list is in another list finndude 4 1,949 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  [solved] Sort list paul18fr 5 2,978 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,593 Aug-12-2021, 04:25 PM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020