Home > Enterprise >  Validating values present in Python List using in operator
Validating values present in Python List using in operator

Time:04-18

I have written below simple code to check values present in Python List (unique_status):

if "Match" in unique_status and "Mismatch" in unique_status:
    print("Match and Mismatch both found.")
elif "Match" in unique_status:
    print("Only Match found.")
elif "Mismatch" in unique_status:
    print("Only Mismatch found.")
else:
    print("Something else is also present.)

For Value in Unique_Status = ['Match'], I am getting "Only Match Found"
For Value in Unique_Status = ['Mismatch'], I am getting "Only Mismatch Found"
For Value in Unique_Status = ['Match','Mismatch'], I am getting "Match and Mismatch both found."

However when list contains some other value also, like ['Match','Mismatch','XYZ'], then else part is not getting executed. What condition/modification is required in my code so that it checks the Unique_Status List, and executes the else condition, in case some other value is also present apart from Match and Mismatch.

CodePudding user response:

if "Match" in unique_status and "Mismatch" in unique_status:
    print("Match and Mismatch both found.")
elif "Match" in unique_status:
    print("Only Match found.")
elif "Mismatch" in unique_status:
    print("Only Mismatch found.")

if any( word not in ['Match','Mismatch'] for word in unique_status ):
    print("Something else is also present.)

CodePudding user response:

Because your first if condition is getting satisfied if unique_status = ['Match','Mismatch','XYZ'] Hence, the else is not working

  • Related