0

I'm trying to write simple code in order to see if a string is in a list. This is the code I've gotten.

a = ["Hill, Lauren", "Smith, Jerry"]
 
b = "Smith"
 
if b in a:
    print("True")
else:
    print("False")

I don't understand why it prints "False". I thought it would print "True" as "Smith" is in "Smith, Jerry". Can someone explain why this happens?

  • 1
    `Smith` and `"Smith, Jerry"` are not same. You can write it as `if any(b in x for x in a)` – ThePyGuy Oct 24 '22 at 03:28
  • `in` checks for exact match when you check something in list (see https://stackoverflow.com/questions/9542738/python-find-in-list) – fas Oct 24 '22 at 03:29
  • 1
    "I thought it would print "True" as "Smith" is in "Smith, Jerry"" - yeah, but you asked Python whether `"Smith"` was in the list, not whether it was in any of the list's elements, and `in` doesn't recurse implicitly like that. – user2357112 Oct 24 '22 at 03:29

5 Answers5

0

a = ["Hill, Lauren", "Smith, Jerry"]

b = "Smith"

if b in a: print("True") else: print("False")

Problem When you observe your last string properly you have created Smith,Jerry combined. When you are checking for b in list, It answers false because its not actually not in list

Solution Correct your list and then run it will not give you an error

Corrected Code

a = ["Hill, Lauren", "Smith", "Jerry"]

b = "Smith"

if b in a: print("True") else: print("False")

0

You ask the interpreter to look if a String ("Smith") is in a list["Hill, Lauren", "Smith, Jerry"], which is not true.

However, if you ask if a String ("Smith") is contained in "Smith, Jerry" this will be true.

If you want this to happen anyway:

a = ["Hill, Lauren", "Smith, Jerry"]
 
b = "Smith"

for ele in a:
    if b in ele:
        print("True")
    else:
        print("False")
0

"Smith" is in "Smith, Jerry", but "Smith, Jerry" is in a list. in only works one layer, so to get the result you want you can try

a = ["Hill, Lauren", "Smith, Jerry"]
 
b = "Smith"

if any(map(lambda x: b in x, a)):
    print("True")
else:
    print("False")

# or
if any([b in i for i in a]):
    print("True")
else:
    print("False")

Allan J.
  • 471
  • 4
  • 15
0

When you do it like that it is looking for an exact match.

if b in a:    #checks the list 'a' for value 'b'
    print("True")
else:
    print("False")

If you want it to be true for partial matches, you would have to do something like this:

if any(b in x for x in a):    #checks each of the elements of list 'a' for value 'b'
    print("True")
else:
    print("False")
A. Trevelyan
  • 136
  • 5
0

Your first list is have 2 value : they are

  1. "Hill, Lauren" and
  2. "Smith, Jerry"

your code will work if the list is like this:

a = ["Hill", "Lauren", "Smith", "Jerry"]

if you want to get the true value, there are some ways:

  1. first you can convert you list to correct list as I said above. then run your code
  2. if you only check the name string, if exists or no , you can use regex expression
  3. or if your list is fixed as above, you can use this:
 if any(map(lambda x: b in x, a)):
        print("True")
    else:
        print("False")