Python Forum
[solved] list content check - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [solved] list content check (/thread-41380.html)



[solved] list content check - paul18fr - Jan-03-2024

Hi,

I've a very naïve question, is there a simplier way to test a list content?b Smile
(solution#2 does the job in a single line)

Paul

MyList = ['xxXx', 'YyYy']

# solution 1
MyList_intermediate = [element.lower() for element in MyList]
Check1 = True if "zzzz" in MyList_intermediate else False
Check2 = True if "xxxx" in MyList_intermediate else False

# solution 2
Check3 = True if True in [True if ("zzzz" in elem.lower()) else False for elem in MyList ] else False
Check4 = True if True in [True if ("xxxx" in elem.lower()) else False for elem in MyList ] else False



RE: list content check - buran - Jan-03-2024

(Jan-03-2024, 02:10 PM)paul18fr Wrote: is there a simplier way to test a list content?b Smile
(solution#2 does the job in a single line)
It's unclear what exactly you try to check
>>> spam = ['xxx', 'yyy', 'zzz']
>>> 'zzz' in spam
True
>>> any('zz' in item for item in spam)
True
>>> any('aa' in item for item in spam)
False
>>>



RE: list content check - paul18fr - Jan-03-2024

oups ... you're right, the list was missing (copy/paste error)

# solution #3 ... better
MyList = ['xxXx', 'YyYy']
Check5 = any('zzzz' in elem.lower() for elem in MyList)
Check6 = any('xxxx' in elem.lower() for elem in MyList)
print(f"'zzzz' in [{', '.join(MyList)}](lower case): {Check5} and 'xxxx': {Check6}")



RE: list content check - deanhystad - Jan-03-2024

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.


RE: list content check - Pedroski55 - Jan-04-2024

As buran showed, you don't need Check1.

Presumably, if you find the string, or strings, you want, you will do something.

MyList = ['xxXx', 'YyYy']

def doSomething():
    return "Positive result"

if 'xxXx' in MyList:
    print(doSomething())

if 'xxXx' and 'YyYy' in MyList:
    print(doSomething())

if 'xxXx' and 'YyYy' and 'zZzZ' in MyList:
    print(doSomething())



RE: list content check - paul18fr - Jan-04-2024

I was looking for the simpliest way, and any is the best solution (i totally forgot it).

Thanks to all


RE: [solved] list content check - deanhystad - Jan-04-2024

any seems the wrong logic to me. Don't you want to check if MyList has both xxxx and yyyy? all is the correct logic for that.