Joshua Stokes Joshua Stokes - 10 months ago 64
Python Question

Detect where characters are in multiple lists

I want to be able to search a string for characters in different lists.

For example:

list1=['a']
list2=['b']


If a user enters in 'a' for example. Python should print 'list1'
and for 'b' list2.

Right now i'm trying to use this function to query the list, but I have to query each list in two separate if statements, like this.

def char_search(input_str):
for char in input_str:
if char in list1:
print("lower")
if char in list2:
print("Upper")


I want to be able to combine the two if statements, and only print something if both conditions are True.

Update Thanks for the prompt replies, however I need to combine if statements.

Example: Only print a response if a string contains characters from both list 1 and 2

Input='a' should not work, nor Input='b', but if user enters 'ab', the repsonse should be something like 'both lists have been matched'. Sorry if I didn't make that clear at the start. The above code was only meant to represent the two different if statements.

Answer Source
list1 = ['a']
list2 = ['b'] 

if (input[0] in list1 or input[1] in list1)  and (input[0] in list2 or input[1] in list2): 
    print "Choice listed"

This works if you want to check that both characters are in at least one list. For more input characters you can add some simple loop logic