-1

I am making a program that is like AI that responds back. I started coding this project but hit a roadblock. I wanted to take a list of words, then check if a given string is inside a list while returning a bool. My code looks something like this...

listofhellos = ['hello', 'hi', 'hey!!!']
if listofhellos.lower() == 'hello': 
# This checks if string 'hello' is in listofhellos
    print('Hey, how are you?')
    # if it is in the list then print out a response

Thank You!

Joël
  • 2,723
  • 18
  • 36
  • `if 'hello' in listofhellos` – shashwat Apr 22 '20 at 05:25
  • Hi! Though the linked question is related, your request is about checking each item in sequence (using `.lower()`, but maybe any other function), so it's a subtly different request. I edited your title to reflect this. – Joël Apr 24 '20 at 08:52

3 Answers3

0

Here's the Solution:

listofhellos = ['hello', 'hi', 'hey!!!']
check = "Hello"
if check.lower() in listofhellos:
    print('Exists')
else:
    print("Not exists")
Stack Kiddy
  • 556
  • 1
  • 4
  • 10
  • Seems ok, but what if `listofhellos` comes from a user, that puts uppercase character at beginning of each word? – Joël Apr 24 '20 at 08:54
0

You can try -

if 'hello' in map(str.lower, ['hello', 'hi', 'Hey!!!']):

or

return 'hello' in map(str.lower, ['hello', 'hi', 'Hey!!!']):```
Prethi G
  • 54
  • 3
-1

You may want to use any() built-in function in conjonction with the Python 'list comprehension' (well,in fact a generator expression):

if any(item.lower() == 'hello' for item in listofhellos):
    print('hey, how are you?')

This will go over each items of your list in order, test it, and return True at the first item that verifies your test.

Joël
  • 2,723
  • 18
  • 36