Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] list content check
#1
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
Reply
#2
(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
>>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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}")
Reply
#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
#5
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())
Reply
#6
I was looking for the simpliest way, and any is the best solution (i totally forgot it).

Thanks to all
Reply
#7
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.
Reply


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 419 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,432 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,130 Jan-28-2023, 12:53 PM
Last Post: Winfried
  Check if clients are online with ips stored in json [SOLVED] AlphaInc 6 2,501 Jun-27-2022, 08:28 AM
Last Post: AlphaInc
  check if element is in a list in a dictionary value ambrozote 4 1,991 May-11-2022, 06:05 PM
Last Post: deanhystad
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,003 May-11-2022, 02:23 PM
Last Post: menator01
  [solved] Basic question on list matchiing paul18fr 7 1,875 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  How to check if a list is in another list finndude 4 1,846 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  [solved] Sort list paul18fr 5 2,898 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,518 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