-1

I'm creating a project in Python where I have a list of strings as follows:

lst_characters = ['Abel', 'Abigail', 'Adon', 'Akira', 'Akuma', 'Alex', 'Balrog', 'Birdie', 'Blanka', 'C. Viper']

Then, I'm checking if a given string "let's say: Akuma (foe)" is in lst_characters.

The way I'm doing it is:

# Sample string to check if exists in the list: 
character = "Akuma (foe)"

# Lower all strings in the list: 
lst_characters = [x.lower().strip() for x in lst_characters]

# Lower the sample string and check if exists in the list: 
if (character.lower() in lst_characters): 
    print("Character (" + character + ") is in the list")
else: 
    print("Character (" + character + ") not found in the list")

And the result is:

Character (Akuma (foe)) not found in the list

You can run this code here - mycompiler.io.

I tried this code - "using any" and this code - using map and none of the code shown in the answers shows me the desired result, that is:

Show that there is an element called (Akuma) in the lst_characters that matches with the sample input Akuma (foe).

  • 5
    `Akuma (foe)` is not in the list of characters. `Akuma` is though – jkr Jan 26 '22 at 16:49
  • 3
    You need to check it the other way because of the reason mentioned by @jakub: `[x for x in lst_characters if x in "Akuma (foe)"]` – It_is_Chris Jan 26 '22 at 16:50
  • 1
    It appears you do not know how the `in` operator works. I suggest learning about it. The way it works is that it loops through the whole list, and compares each list item to your input, to see if they are equal. `character in lst` is equivalent to `any([character == lst_item for lst_item in lst])`. Since there is no item in `lst_characters` that is literally equal to `'akuma (foe)'`, the `in` operator naturally doesn't return `True`. – Random Davis Jan 26 '22 at 16:53
  • Out of curiosity, why is `character` not a tuple like `("Akuma", "foe")`? You seem to be conflating data structures with how you would *display* a particular data structure. – chepner Jan 26 '22 at 16:53
  • 1
    change if (character.lower() in lst_characters): to if ((character.split())[0].lower() in lst_characters): Might not be the best way in future but this works for your code – Will Jordan Jan 26 '22 at 16:54
  • @RandomDavis thank you, Your explanation about the `in` operator is very clear - I'm self-learning Python :) – Marco Aurelio Fernandez Reyes Jan 26 '22 at 16:55
  • @chepner I'm self-learning Python and this is a little project I'm working on. I need to check more examples and the documentation for another data structures. – Marco Aurelio Fernandez Reyes Jan 26 '22 at 16:58

2 Answers2

2

You're checking if the string Akuma (foe) exists in your list. It doesn't. Akuma however does.

You could turn your check around like this:

exists = any(x in character.lower() for x in lst_characters)
if exists: 
    print("Character (" + character + ") is in the list")
else: 
    print("Character (" + character + ") not found in the list")
Tobi208
  • 1,306
  • 10
  • 17
0
if (character.lower() in lst_characters): 

to

if ((character.split())[0].lower() in lst_characters): 
Will Jordan
  • 245
  • 2
  • 12
  • Interesting. The `character.split()` is equivalent to separate the string by spaces. Thank you. +1 – Marco Aurelio Fernandez Reyes Jan 26 '22 at 17:02
  • 1
    this wouldn't work if the character was `"C. Viper"`, which is in the list of characters. – jkr Jan 26 '22 at 18:24
  • @jakub if he wanted to keep it super simple he could just use a blank space character to join them into one string, or change to C.Viper. This solution wasn't perfect I know but I think it is still useful for him to see – Will Jordan Jan 27 '22 at 12:11